@NotNull public static String getPackage(@NotNull PsiFile file) { Project project = file.getProject(); String modulePath = project.getBasePath() + FILE_SEPARATOR; String filePath = file.getVirtualFile().getPath(); filePath = filePath.replace(modulePath, ""); if (!filePath.contains(FILE_SEPARATOR)) { return ""; } int index = filePath.indexOf(FILE_SEPARATOR); return filePath.substring(0, index); }
@Nullable private static GoArgumentList getList(@NotNull ParameterInfoContext context) { PsiElement at = context.getFile().findElementAt(context.getOffset()); return PsiTreeUtil.getParentOfType(at, GoArgumentList.class); }
@Override @NotNull protected String getConfigurationName(@NotNull PsiFile file) { return "go run " + file.getName(); } }
@Nullable protected static String getPath(@Nullable PsiFile file) { if (file == null) return null; VirtualFile virtualFile = file.getOriginalFile().getVirtualFile(); return virtualFile == null ? null : virtualFile.getPath(); }
@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; } }
public void testPackage_test() { PsiFile file = myFixture.addFileToProject("import/path/package_test.go", "package ma<caret>in; import `gopkg.in/check.v1`"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); doTestProducedConfigurations(myFixture.getFile().findElementAt(myFixture.getCaretOffset())); }
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); } }
public void testDoNotConsiderImportFromTestPackageAsSelfImport() { PsiFile file = myFixture.addFileToProject("path/a_test.go", "package pack_test; import <caret>\"path\""); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); myFixture.checkHighlighting(); }
@NotNull @Override public Collection<PsiFileSystemItem> computeDefaultContexts() { PsiFile file = getContainingFile(); if (file == null || !file.isValid() || isAbsolutePathReference()) { return Collections.emptyList(); } PsiManager psiManager = file.getManager(); Module module = ModuleUtilCore.findModuleForPsiElement(file); Project project = file.getProject(); LinkedHashSet<VirtualFile> sourceRoots = GoVendoringUtil.isVendoringEnabled(module) ? GoSdkUtil.getVendoringAwareSourcesPathsToLookup(project, module, file.getVirtualFile()) : GoSdkUtil.getSourcesPathsToLookup(project, module); return ContainerUtil.mapNotNull(sourceRoots, psiManager::findDirectory); }
@Override protected void postProcess(PsiFile createdElement, String templateName, Map<String, String> customProperties) { if (createdElement instanceof GoFile) { GoPackageClause packageClause = ((GoFile)createdElement).getPackage(); if (packageClause == null) { return; } Project project = createdElement.getProject(); Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) { return; } VirtualFile virtualFile = createdElement.getContainingFile().getVirtualFile(); if (virtualFile == null) { return; } if (FileDocumentManager.getInstance().getDocument(virtualFile) == editor.getDocument()) { editor.getCaretModel().moveToOffset(packageClause.getTextRange().getEndOffset()); } } }
@NotNull private static String convertType(@NotNull PsiFile file, @Nullable GoType type, @NotNull Map<String, GoImportSpec> importMap) { if (type == null) return GoConstants.INTERFACE_TYPE; Module module = ModuleUtilCore.findModuleForPsiElement(file); boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(module); return GoDocumentationProvider.getTypePresentation(type, element -> { if (element instanceof GoTypeSpec) { GoTypeSpec spec = (GoTypeSpec)element; if (GoPsiImplUtil.builtin(spec)) return spec.getIdentifier().getText(); GoFile typeFile = spec.getContainingFile(); if (file.isEquivalentTo(typeFile) || GoUtil.inSamePackage(typeFile, file)) { return spec.getIdentifier().getText(); } if (!spec.isPublic()) { return GoConstants.INTERFACE_TYPE; } GoPathScopeHelper scopeHelper = GoPathScopeHelper.fromReferenceFile(file.getProject(), module, file.getVirtualFile()); boolean isAllowed = scopeHelper.couldBeReferenced(typeFile.getVirtualFile(), file.getVirtualFile()); if (!isAllowed) return GoConstants.INTERFACE_TYPE; String importPath = typeFile.getImportPath(vendoringEnabled); GoImportSpec importSpec = importMap.get(importPath); String packageName = StringUtil.notNullize(typeFile.getPackageName()); String qualifier = StringUtil.notNullize(GoPsiImplUtil.getImportQualifierToUseInFile(importSpec, packageName), packageName); // todo: add import package fix if getImportQualifierToUseInFile is null? return GoPsiImplUtil.getFqn(qualifier, spec.getIdentifier().getText()); } return GoConstants.INTERFACE_TYPE; }); }
@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 Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) { if (!file.getLanguage().is(BallerinaLanguage.INSTANCE)) { return Result.Continue; Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); PsiElement element = file.findElementAt(caretOffset);
public static void assertExit(PsiFile file) { final String text = file.getText(); Object exception = null; try { Quoter.quote(text); } catch (IOException | OtpErlangDecodeException | OtpErlangExit e) { exception = e; } assertThat(exception, instanceOf(OtpErlangExit.class)); }
public void testMethodWithTransitiveImport() { myFixture.addFileToProject("a.go", "package main; import `middle`; func main() { fmt.Println(middle.A.Method()) }"); myFixture.addFileToProject("middle/middle.go", "package middle; import `declaration`; var A *declaration.D = nil"); PsiFile file = myFixture.addFileToProject("declaration/declaration.go", "package declaration; type D struct {}; func (D) Met<caret>hod() {}"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); Collection<UsageInfo> usages = myFixture.findUsages(myFixture.getElementAtCaret()); assertEquals(1, usages.size()); //noinspection ConstantConditions assertEquals("a.go", usages.iterator().next().getFile().getName()); }
@Nullable @Override public Lexer getIndexingLexer(@NotNull PsiFile file) { return file instanceof GoFile ? ((GoFile)file).getParserDefinition().createLexer(file.getProject()) : null; }
/** * @see fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil#getElementOnTwigViewProvider */ public void testGetTransDefaultDomainOnInjectedElement() { myFixture.configureByText("foo.html.twig", "" + "{% trans_default_domain \"foo\" %}\n" + "<a href=\"#\">FOO<caret>BAR</a>" ); PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); assertTrue( TwigUtil.getElementOnTwigViewProvider(psiElement).getContainingFile().getFileType() == TwigFileType.INSTANCE ); }
@NotNull @Override public DataIndexer<String, DispatcherEvent, FileContent> getIndexer() { return inputData -> { Map<String, DispatcherEvent> map = new HashMap<>(); PsiFile psiFile = inputData.getPsiFile(); if(!Symfony2ProjectComponent.isEnabledForIndex(psiFile.getProject())) { return map; } psiFile.accept(new MyPsiRecursiveElementWalkingVisitor(map)); return map; }; }
@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); } }); } } } }
/** * 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(); }