@Override public boolean autoImportReferenceAt(@NotNull Editor editor, @NotNull PsiFile file, int offset) { if (!file.getViewProvider().getLanguages().contains(GoLanguage.INSTANCE)) { return false; } PsiReference reference = file.findReferenceAt(offset); if (reference != null) { return new GoImportPackageQuickFix(reference).doAutoImportOrShowHint(editor, false); } return false; } }
@NotNull private static String excerpt(@NotNull PsiFile containingFile, @NotNull PsiElement element) { StringBuilder excerptBuilder = new StringBuilder(); excerptBuilder.append('\n'); excerptBuilder.append("### Excerpt\n"); excerptBuilder.append('\n'); excerptBuilder.append("```\n"); excerptBuilder.append(element.getText()); excerptBuilder.append('\n'); excerptBuilder.append("```\n"); FileViewProvider fileViewProvider = containingFile.getViewProvider(); Document document = fileViewProvider.getDocument(); if (document != null) { TextRange textRange = element.getTextRange(); int startingLine = document.getLineNumber(textRange.getStartOffset()); int endingLine = document.getLineNumber(textRange.getEndOffset()); excerptBuilder.append(" Line(s) "); excerptBuilder.append(startingLine); excerptBuilder.append('-'); excerptBuilder.append(endingLine); VirtualFile virtualFile = containingFile.getVirtualFile(); if (virtualFile != null) { excerptBuilder.append(" in "); excerptBuilder.append(virtualFile.getPath()); } excerptBuilder.append("\n"); } return excerptBuilder.toString(); }
@Override public void apply(@NotNull PsiFile file, @NotNull List<Issue> issueList, @NotNull AnnotationHolder holder) { if (issueList.size() > 0) { @Nullable Document document = file.getViewProvider().getDocument(); if (document != null) { String workingDirectory = ensureWorkingDirectory(file.getProject()); for (Issue issue : issueList) { int lineStartOffset = document.getLineStartOffset(issue.line); int start; int end; if (issue.column != null) { start = lineStartOffset + issue.column; end = start + 1; } else { start = lineStartOffset; end = document.getLineEndOffset(issue.line); } Annotation annotation = holder.createWarningAnnotation(new TextRange(start, end), issue.message); annotation.setAfterEndOfLine(end == start); issue.explanation.ifPresent(explanation -> { String toolTip = explanationToToolTip(explanation, workingDirectory); if (!toolTip.isEmpty()) { annotation.setTooltip(toolTip); } }); } } } }
private List<PsiElement> localErrors(@NotNull Language language) { final FileViewProvider fileViewProvider = myFile.getViewProvider(); PsiFile root = fileViewProvider.getPsi(language); final List<PsiElement> errorElementList = new LinkedList<>(); root.accept( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof PsiErrorElement) { errorElementList.add(element); } super.visitElement(element); } } ); return errorElementList; }
@Override public boolean autoImportReferenceAtCursor(@NotNull Editor editor, @NotNull PsiFile file) { if (!file.getViewProvider().getLanguages().contains(GoLanguage.INSTANCE) || !DaemonListeners.canChangeFileSilently(file)) { return false; } int caretOffset = editor.getCaretModel().getOffset(); Document document = editor.getDocument(); int lineNumber = document.getLineNumber(caretOffset); int startOffset = document.getLineStartOffset(lineNumber); int endOffset = document.getLineEndOffset(lineNumber); List<PsiElement> elements = CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset); for (PsiElement element : elements) { if (element instanceof GoCompositeElement) { for (PsiReference reference : element.getReferences()) { GoImportPackageQuickFix fix = new GoImportPackageQuickFix(reference); if (fix.doAutoImportOrShowHint(editor, false)) { return true; } } } } return false; }
/** * Handles an action of adding new template. * Ignores action if selected file is not a {@link IgnoreFile} instance, otherwise shows GeneratorDialog. * * @param e action event */ @Override public void actionPerformed(@NotNull AnActionEvent e) { final Project project = e.getData(CommonDataKeys.PROJECT); final PsiFile file = e.getData(CommonDataKeys.PSI_FILE); if (project == null || !(file instanceof IgnoreFile)) { return; } String content = file.getText(); Document document = file.getViewProvider().getDocument(); if (document != null) { Editor[] editors = EditorFactory.getInstance().getEditors(document); if (editors.length > 0) { String selectedText = editors[0].getSelectionModel().getSelectedText(); if (!StringUtil.isEmpty(selectedText)) { content = selectedText; } } } new UserTemplateDialog(project, content).show(); }
private void doTest(File resource) { try { String name = resource.getName().replace("." + myFileExt, EMPTY_STRING); // Retrieves relative path of the file since loadFile() uses "myFullDataPath" as the source root. String relativeFilePath = resource.getPath().replace(getTestDataPath(), EMPTY_STRING); String text = loadFile(relativeFilePath); myFile = createPsiFile(name, text); ensureParsed(myFile); assertEquals("light virtual file text mismatch", text, ((LightVirtualFile) myFile.getVirtualFile()).getContent().toString()); assertEquals("virtual file text mismatch", text, LoadTextUtil.loadText(myFile.getVirtualFile())); assertEquals("doc text mismatch", text, myFile.getViewProvider().getDocument().getText()); assertEquals("psi text mismatch", text, myFile.getText()); ensureCorrectReparse(myFile); checkResult(relativeFilePath, myFile); } catch (IOException e) { throw new RuntimeException(e); } }
@NotNull private PsiFile root() { String name = getTestName(false); PsiFile root; try { String text = loadFile(name + "." + myFileExt); myFile = createPsiFile(name, text); ensureParsed(myFile); assertEquals("light virtual file text mismatch", text, ((LightVirtualFile) myFile.getVirtualFile()).getContent().toString()); assertEquals("virtual file text mismatch", text, LoadTextUtil.loadText(myFile.getVirtualFile())); assertEquals("doc text mismatch", text, myFile.getViewProvider().getDocument().getText()); assertEquals("psi text mismatch", text, myFile.getText()); FileViewProvider fileViewProvider = myFile.getViewProvider(); root = fileViewProvider.getPsi(ElixirLanguage.INSTANCE); assertNotNull(root); } catch (IOException exception) { throw new RuntimeException(exception); } return root; } }
/** * Resolve html language injection */ @Nullable public static PsiElement getElementOnTwigViewProvider(@NotNull PsiElement element) { PsiFile file = element.getContainingFile(); TextRange textRange = element.getTextRange(); return file.getViewProvider().findElementAt(textRange.getStartOffset(), TwigLanguage.INSTANCE); }
@Override public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, @Nullable EditorActionHandler originalHandler) { if (file.getViewProvider() instanceof EmbeddedPerlFileViewProvider) { EmbeddedPerlSmartKeysUtil.addCloseMarker(editor, file, "\n?>"); } return Result.Continue; }
@NotNull @Override public Result charTyped(char c, @NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) { if (file.getViewProvider() instanceof EmbeddedPerlFileViewProvider) { if (c == ' ') { EmbeddedPerlSmartKeysUtil.addCloseMarker(editor, file, " ?>"); } } return super.charTyped(c, project, editor, file); } }
@NotNull @Override public Result charTyped(char c, @NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) { if (file.getViewProvider() instanceof MojoliciousFileViewProvider) { if (c == ' ') { MojoliciousSmartKeysUtil.addCloseMarker(editor, file, " " + KEYWORD_MOJO_BLOCK_CLOSER); } } return super.charTyped(c, project, editor, file); } }
@Override public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (ApplicationManager.getApplication().isUnitTestMode()) { // builds 163.x set the read-only flag for unknown reasons, work around it in tests return true; } Document document = file.getViewProvider().getDocument(); return document != null && document.isWritable(); }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { // Replace this position with the same value, we have to trigger a reparse somehow Document document = file.getViewProvider().getDocument(); if (document != null && document.isWritable()) { PsiElement replacement = BashPsiElementFactory.createComposedVar(project, variableName); startElement.replace(replacement); } } }
public PerlFormattingContext(@NotNull PsiElement element, @NotNull CodeStyleSettings settings) { mySettings = settings.getCommonSettings(PerlLanguage.INSTANCE); myPerlSettings = settings.getCustomSettings(PerlCodeStyleSettings.class); mySpacingBuilder = createSpacingBuilder(); PsiFile containingFile = element.getContainingFile(); myDocument = containingFile == null ? null : containingFile.getViewProvider().getDocument(); myCommentsLines = myDocument == null ? new BitSet() : new BitSet(myDocument.getLineCount()); }
@Override protected boolean notSameFile(@Nullable VirtualFile file, @NotNull PsiFile containingFile) { final PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument()); if (currentFile == null) { return true; } InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject()); PsiFile topLevelFile = manager.getTopLevelFile(containingFile); PsiFile topLevelFile1 = manager.getTopLevelFile(currentFile); return topLevelFile == null || topLevelFile1 == null || topLevelFile.getViewProvider() != topLevelFile1.getViewProvider(); }
@Nullable @Override public HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file) { int offset = TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset()); PsiElement target = file.getViewProvider().findElementAt(offset, PerlLanguage.INSTANCE); return target == null ? null : new PerlHighlightUsagesHandler(editor, file, target, offset); }
@Override public boolean value(PsiElement element) { return !(element instanceof OuterLanguageElementImpl) || !element.getContainingFile().getViewProvider().getBaseLanguage().isKindOf(PerlLanguage.INSTANCE); } }
public static int getElementEndLineNumber(PsiElement element) { FileViewProvider fileViewProvider = element.getContainingFile().getViewProvider(); if (fileViewProvider.getDocument() != null) { return fileViewProvider.getDocument().getLineNumber(element.getTextOffset() + element.getTextLength()) + 1; } return 0; }
@NotNull protected PsiElement getElementAtCaretWithoutInjection() { PsiElement result = getFile().getViewProvider().findElementAt(getEditor().getCaretModel().getOffset()); assertNotNull(result); PsiFile leafFile = result.getContainingFile(); if (InjectedLanguageManager.getInstance(getProject()).isInjectedFragment(leafFile)) { result = leafFile.getContext(); } assertNotNull(result); return result; }