@Nullable @Override public Lexer getIndexingLexer(@NotNull PsiFile file) { return file instanceof GoFile ? ((GoFile)file).getParserDefinition().createLexer(file.getProject()) : null; }
@Nullable @Override public Lexer getIndexingLexer(@NotNull PsiFile file) { if (file instanceof BallerinaFile) { return ((BallerinaFile) file).getParserDefinition().createLexer(file.getProject()); } return null; }
@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); }
@NotNull private GeneralCommandLine generalCommandLine(@NotNull PsiFile file) throws MissingSdk { Project project = file.getProject(); Module module = ModuleUtilCore.findModuleForPsiElement(file); return generalCommandLine( project, module, mixParametersList(file) ); }
private void perform(@NotNull PsiFile file, @Nullable String pathToImport) { if (file instanceof GoFile && pathToImport != null) { Project project = file.getProject(); CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> { if (!isAvailable()) return; if (((GoFile)file).getImportedPackagesMap().containsKey(pathToImport)) return; ((GoFile)file).addImport(pathToImport, null); }), "Add import", null); } }
@Nullable @Override public List<Issue> doAnnotate(PsiFile file) { List<Issue> issueList; try { ProcessOutput processOutput = ExecUtil.execAndGetOutput(generalCommandLine(file)); Project project = file.getProject(); Module module = ModuleUtilCore.findModuleForPsiElement(file); issueList = lineListToIssueList(processOutput.getStdoutLines(), project, module); } catch (ExecutionException | MissingSdk executionException) { issueList = emptyList(); } return issueList; }
@Override public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { if (!(file instanceof GoFile)) { return; } String testingQualifier = null; if (myType.getParamType() != null) { testingQualifier = importTestingPackageIfNeeded((GoFile)file); PsiDocumentManager.getInstance(file.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument()); } String functionText = "func " + myType.getPrefix(); int offset = functionText.length(); functionText += "(" + myType.getSignature(testingQualifier) + ") {\n\t\n}"; EditorModificationUtil.insertStringAtCaret(editor, functionText, false, offset); AutoPopupController.getInstance(project).scheduleAutoPopup(editor); }
@Override public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { // We only perform this action on Ballerina modules since this might cause issues in other modules. Module module = ModuleUtil.findModuleForFile(file.getVirtualFile(), file.getProject()); boolean isBallerinaModule = BallerinaSdkService.getInstance(project).isBallerinaModule(module); if (!isBallerinaModule) { return false; } // If the file contain error elements, do not perform auto import. Since this might affect badly to the user // experience. Collection<PsiErrorElement> errorElements = PsiTreeUtil.findChildrenOfType(file, PsiErrorElement.class); if (!errorElements.isEmpty()) { return false; } PsiReference reference = startElement.getReference(); return file instanceof BallerinaFile && file.getManager().isInProject(file) && (reference == null || reference.resolve() == null); }
@Override public void run() { if (!importEntriesToDelete.isEmpty() || !importIdentifiersToDelete.isEmpty()) { PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject()); Document document = manager.getDocument(file); if (document != null) { manager.commitDocument(document); } } for (PsiElement importEntry : importEntriesToDelete) { if (importEntry != null && importEntry.isValid()) { deleteImportSpec(getImportSpec(importEntry)); } } for (PsiElement identifier : importIdentifiersToDelete) { if (identifier != null && identifier.isValid()) { identifier.delete(); } } } };
@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); } }); } } } }
@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); }
boolean isOnTheFly) { final VirtualFile virtualFile = file.getVirtualFile(); if (!(file instanceof IgnoreFile) || !Utils.isInProject(virtualFile, file.getProject())) { return null; ((IgnoreFile) file).findChildrenByClass(IgnoreEntry.class) )); final MatcherUtil matcher = IgnoreManager.getInstance(file.getProject()).getMatcher(); final Map<IgnoreEntry, Set<String>> matchedMap = getPathsSet(contextDirectory, entries, matcher);
public static GlobalSearchScope create(@NotNull PsiElement declarationContext, boolean filterByImportList) { if (declarationContext instanceof GoNamedElement && ((GoNamedElement)declarationContext).isBlank()) { return GlobalSearchScope.EMPTY_SCOPE; } PsiFile declarationPsiFile = declarationContext.getContainingFile(); if (!(declarationPsiFile instanceof GoFile)) { return GlobalSearchScope.fileScope(declarationPsiFile); } if (GoPsiImplUtil.isBuiltinFile(declarationPsiFile)) { return GlobalSearchScope.allScope(declarationContext.getProject()); } VirtualFile declarationFile = declarationPsiFile.getVirtualFile(); if (declarationFile == null || declarationFile.getParent() == null) { return GlobalSearchScope.fileScope(declarationPsiFile); } return new GoPathUseScope(declarationPsiFile.getProject(), declarationFile, filterByImportList); }
Project project = file.getProject();
@Override public ReturnResult beforeCheckin(@Nullable CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) { if (enabled(panel)) { Ref<Boolean> success = Ref.create(true); FileDocumentManager.getInstance().saveAllDocuments(); for (PsiFile file : getPsiFiles()) { VirtualFile virtualFile = file.getVirtualFile(); new GoFmtFileAction().doSomething(virtualFile, ModuleUtilCore.findModuleForPsiElement(file), file.getProject(), "Go fmt", true, result -> { if (!result) success.set(false); }); } if (!success.get()) { return showErrorMessage(executor); } } return super.beforeCheckin(); }
@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; }); }
Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
@Override public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) { if (!file.getLanguage().is(BallerinaLanguage.INSTANCE)) { return Result.Continue; } // We need to save the file before checking. Otherwise issues can occur when we press enter in a string. Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); // Checks whether the previous line starts with "#". LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition(); int prevLine = caretPos.line - 1; String lineString = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(prevLine), editor.getDocument().getLineEndOffset(prevLine))); if (lineString.trim().startsWith("#")) { int newCol = lineString.indexOf("#"); String enteredText = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line))).trim(); editor.getDocument().deleteString(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line)); editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretPos.line, 1)); enterNewLine(editor, enteredText, newCol); // Commit the document. PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } return Result.Continue; }
@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()); } } }
public Optional<SwaggerFileType> getFileType(final CompletionParameters parameters) { final Project project = parameters.getOriginalFile().getProject(); final VirtualFile virtualFile = parameters.getOriginalFile().getVirtualFile(); return getFileType(project, virtualFile); }