Search in sources :

Example 26 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoFmtCheckinFactory method createHandler.

@Override
@NotNull
public CheckinHandler createHandler(@NotNull CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
    return new CheckinHandler() {

        @Override
        public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
            JCheckBox checkBox = new JCheckBox("Go fmt");
            return new RefreshableOnComponent() {

                @Override
                @NotNull
                public JComponent getComponent() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(checkBox, BorderLayout.WEST);
                    return panel;
                }

                @Override
                public void refresh() {
                }

                @Override
                public void saveState() {
                    PropertiesComponent.getInstance(panel.getProject()).setValue(GO_FMT, Boolean.toString(checkBox.isSelected()));
                }

                @Override
                public void restoreState() {
                    checkBox.setSelected(enabled(panel));
                }
            };
        }

        @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 ReturnResult showErrorMessage(@Nullable CommitExecutor executor) {
            String[] buttons = new String[] { "&Details...", commitButtonMessage(executor, panel), CommonBundle.getCancelButtonText() };
            int answer = Messages.showDialog(panel.getProject(), "<html><body>GoFmt returned non-zero code on some of the files.<br/>" + "Would you like to commit anyway?</body></html>\n", "Go Fmt", null, buttons, 0, 1, UIUtil.getWarningIcon());
            if (answer == Messages.OK) {
                return ReturnResult.CLOSE_WINDOW;
            }
            if (answer == Messages.NO) {
                return ReturnResult.COMMIT;
            }
            return ReturnResult.CANCEL;
        }

        @NotNull
        private List<PsiFile> getPsiFiles() {
            Collection<VirtualFile> files = panel.getVirtualFiles();
            List<PsiFile> psiFiles = ContainerUtil.newArrayList();
            PsiManager manager = PsiManager.getInstance(panel.getProject());
            for (VirtualFile file : files) {
                PsiFile psiFile = manager.findFile(file);
                if (psiFile instanceof GoFile) {
                    psiFiles.add(psiFile);
                }
            }
            return psiFiles;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoFile(com.goide.psi.GoFile) PsiManager(com.intellij.psi.PsiManager) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) CommitExecutor(com.intellij.openapi.vcs.changes.CommitExecutor) PairConsumer(com.intellij.util.PairConsumer) RefreshableOnComponent(com.intellij.openapi.vcs.ui.RefreshableOnComponent) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoDocumentationProvider method getDocumentationElementForLink.

@Override
public PsiElement getDocumentationElementForLink(PsiManager psiManager, String link, PsiElement context) {
    if (context != null && !DumbService.isDumb(psiManager.getProject())) {
        // it's important to ask module on file, otherwise module won't be found for elements in libraries files [zolotov]
        Module module = ModuleUtilCore.findModuleForPsiElement(context.getContainingFile());
        int hash = link.indexOf('#');
        String importPath = hash >= 0 ? link.substring(0, hash) : link;
        Project project = psiManager.getProject();
        VirtualFile directory = GoPackageUtil.findByImportPath(importPath, project, module);
        PsiDirectory psiDirectory = directory != null ? psiManager.findDirectory(directory) : null;
        String anchor = hash >= 0 ? link.substring(Math.min(hash + 1, link.length())) : null;
        if (StringUtil.isNotEmpty(anchor)) {
            GlobalSearchScope scope = psiDirectory != null ? GoPackageUtil.packageScope(psiDirectory, null) : GlobalSearchScope.projectScope(project);
            IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
            GoNamedElement element = ContainerUtil.getFirstItem(StubIndex.getElements(GoAllPublicNamesIndex.ALL_PUBLIC_NAMES, anchor, project, scope, idFilter, GoNamedElement.class));
            if (element != null) {
                return element;
            }
            return ContainerUtil.getFirstItem(StubIndex.getElements(GoAllPrivateNamesIndex.ALL_PRIVATE_NAMES, anchor, project, scope, idFilter, GoNamedElement.class));
        } else {
            return psiDirectory;
        }
    }
    return super.getDocumentationElementForLink(psiManager, link, context);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module)

Example 28 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoConsoleFilter method applyFilter.

@Override
public Result applyFilter(@NotNull String line, int entireLength) {
    Matcher goGetMatcher = GO_GET_MESSAGE_PATTERN.matcher(line);
    if (goGetMatcher.find() && myModule != null) {
        String packageName = goGetMatcher.group(2).trim();
        HyperlinkInfo hyperlinkInfo = new GoGetHyperlinkInfo(packageName, myModule);
        int lineStart = entireLength - line.length();
        return new Result(lineStart + goGetMatcher.start(1), lineStart + goGetMatcher.end(2), hyperlinkInfo);
    }
    Matcher matcher = MESSAGE_PATTERN.matcher(line);
    if (!matcher.find()) {
        Matcher fileMatcher = GO_FILE_PATTERN.matcher(line);
        List<ResultItem> resultItems = ContainerUtil.newArrayList();
        while (fileMatcher.find()) {
            VirtualFile file = findSingleFile(fileMatcher.group(1));
            if (file != null) {
                resultItems.add(createResult(line, entireLength, fileMatcher.start(1), fileMatcher.end(1), 0, 0, file));
            }
        }
        return !resultItems.isEmpty() ? new Result(resultItems) : null;
    }
    int startOffset = matcher.start(1);
    int endOffset = matcher.end(2);
    String fileName = matcher.group(1);
    int lineNumber = StringUtil.parseInt(matcher.group(2), 1) - 1;
    if (lineNumber < 0) {
        return null;
    }
    int columnNumber = -1;
    if (matcher.groupCount() > 3) {
        columnNumber = StringUtil.parseInt(matcher.group(4), 1) - 1;
        endOffset = Math.max(endOffset, matcher.end(4));
    }
    Matcher appEnginePathMatcher = APP_ENGINE_PATH_PATTERN.matcher(fileName);
    if (appEnginePathMatcher.find()) {
        fileName = fileName.substring(appEnginePathMatcher.end());
    }
    VirtualFile virtualFile = null;
    if (FileUtil.isAbsolutePlatformIndependent(fileName)) {
        virtualFile = ApplicationManager.getApplication().isUnitTestMode() ? TempFileSystem.getInstance().refreshAndFindFileByPath(fileName) : VirtualFileManager.getInstance().refreshAndFindFileByUrl(VfsUtilCore.pathToUrl(fileName));
    } else {
        if (myWorkingDirectoryUrl != null) {
            virtualFile = VirtualFileManager.getInstance().refreshAndFindFileByUrl(myWorkingDirectoryUrl + "/" + fileName);
        }
        if (virtualFile == null && myModule != null) {
            virtualFile = findInGoPath(fileName);
            if (virtualFile == null && fileName.startsWith("src/")) {
                virtualFile = findInGoPath(StringUtil.trimStart(fileName, "src/"));
            }
        }
        if (virtualFile == null) {
            VirtualFile baseDir = myProject.getBaseDir();
            if (baseDir != null) {
                virtualFile = baseDir.findFileByRelativePath(fileName);
                if (virtualFile == null && fileName.startsWith("src/")) {
                    // exclude src
                    virtualFile = baseDir.findFileByRelativePath(StringUtil.trimStart(fileName, "src/"));
                }
            }
        }
    }
    if (virtualFile == null) {
        virtualFile = findSingleFile(fileName);
    }
    if (virtualFile == null) {
        return null;
    }
    return createResult(line, entireLength, startOffset, endOffset, lineNumber, columnNumber, virtualFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Matcher(java.util.regex.Matcher) OpenFileHyperlinkInfo(com.intellij.execution.filters.OpenFileHyperlinkInfo) HyperlinkInfo(com.intellij.execution.filters.HyperlinkInfo)

Example 29 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoProjectModelConverterProvider method convertSdks.

private static void convertSdks() {
    ProjectJdkTable sdkTable = ProjectJdkTable.getInstance();
    Collection<String> globalGoPathUrls = ContainerUtil.newLinkedHashSet();
    Collection<Sdk> sdksToDelete = ContainerUtil.newArrayList();
    Collection<Sdk> sdksToAdd = ContainerUtil.newArrayList();
    GoSdkType sdkType = GoSdkType.getInstance();
    for (Sdk sdk : sdkTable.getAllJdks()) {
        String sdkTypeName = sdk.getSdkType().getName();
        if (isGoSdkType(sdkTypeName)) {
            sdksToDelete.add(sdk);
            String sdkHome = sdkType.adjustSelectedSdkHome(sdk.getHomePath());
            if (sdkType.isValidSdkHome(sdkHome)) {
                ProjectJdkImpl newSdk = new ProjectJdkImpl(sdk.getName(), sdkType, sdkHome, sdkType.getVersionString(sdkHome));
                sdkType.setupSdkPaths(newSdk);
                sdksToAdd.add(newSdk);
                for (String classesRoot : sdk.getRootProvider().getUrls(OrderRootType.CLASSES)) {
                    if (!classesRoot.equals(sdk.getHomePath())) {
                        globalGoPathUrls.add(classesRoot);
                    }
                }
            }
        }
    }
    for (VirtualFile file : GoSdkUtil.getGoPathsRootsFromEnvironment()) {
        globalGoPathUrls.remove(file.getUrl());
    }
    AccessToken l = WriteAction.start();
    try {
        for (Sdk sdk : sdksToDelete) {
            sdkTable.removeJdk(sdk);
        }
        for (Sdk sdk : sdksToAdd) {
            sdkTable.addJdk(sdk);
        }
        globalGoPathUrls.addAll(GoApplicationLibrariesService.getInstance().getLibraryRootUrls());
        GoApplicationLibrariesService.getInstance().setLibraryRootUrls(globalGoPathUrls);
    } finally {
        l.finish();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectJdkTable(com.intellij.openapi.projectRoots.ProjectJdkTable) AccessToken(com.intellij.openapi.application.AccessToken) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) Sdk(com.intellij.openapi.projectRoots.Sdk) GoSdkType(com.goide.sdk.GoSdkType)

Example 30 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoImportReferenceSet method computeDefaultContexts.

@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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)5443 File (java.io.File)757 Nullable (org.jetbrains.annotations.Nullable)719 Project (com.intellij.openapi.project.Project)718 NotNull (org.jetbrains.annotations.NotNull)703 PsiFile (com.intellij.psi.PsiFile)564 Module (com.intellij.openapi.module.Module)495 IOException (java.io.IOException)327 ArrayList (java.util.ArrayList)258 Document (com.intellij.openapi.editor.Document)241 PsiElement (com.intellij.psi.PsiElement)205 Test (org.junit.Test)193 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)124 PsiDirectory (com.intellij.psi.PsiDirectory)124 XmlFile (com.intellij.psi.xml.XmlFile)124 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)116 Editor (com.intellij.openapi.editor.Editor)114 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)100 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)91 List (java.util.List)90