Search in sources :

Example 86 with SmartList

use of com.intellij.util.SmartList in project intellij-community by JetBrains.

the class DebuggerUtilsEx method collectLambdas.

public static List<PsiLambdaExpression> collectLambdas(@NotNull SourcePosition position, final boolean onlyOnTheLine) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiFile file = position.getFile();
    final int line = position.getLine();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    if (document == null || line >= document.getLineCount()) {
        return Collections.emptyList();
    }
    PsiElement element = position.getElementAt();
    if (element == null) {
        return Collections.emptyList();
    }
    final TextRange lineRange = DocumentUtil.getLineTextRange(document, line);
    do {
        PsiElement parent = element.getParent();
        if (parent == null || (parent.getTextOffset() < lineRange.getStartOffset())) {
            break;
        }
        element = parent;
    } while (true);
    final List<PsiLambdaExpression> lambdas = new SmartList<>();
    final PsiElementVisitor lambdaCollector = new JavaRecursiveElementVisitor() {

        @Override
        public void visitLambdaExpression(PsiLambdaExpression expression) {
            super.visitLambdaExpression(expression);
            if (!onlyOnTheLine || getFirstElementOnTheLine(expression, document, line) != null) {
                lambdas.add(expression);
            }
        }
    };
    element.accept(lambdaCollector);
    for (PsiElement sibling = getNextElement(element); sibling != null; sibling = getNextElement(sibling)) {
        if (!intersects(lineRange, sibling)) {
            break;
        }
        sibling.accept(lambdaCollector);
    }
    // add initial lambda if we're inside already
    PsiElement method = getContainingMethod(element);
    if (method instanceof PsiLambdaExpression && !lambdas.contains(method)) {
        lambdas.add((PsiLambdaExpression) method);
    }
    return lambdas;
}
Also used : Document(com.intellij.openapi.editor.Document) SmartList(com.intellij.util.SmartList) Breakpoint(com.intellij.debugger.ui.breakpoints.Breakpoint)

Example 87 with SmartList

use of com.intellij.util.SmartList in project intellij-community by JetBrains.

the class JavaDocumentationProvider method findUrlForVirtualFile.

@Nullable
public static List<String> findUrlForVirtualFile(@NotNull Project project, @NotNull VirtualFile virtualFile, @NotNull String relPath) {
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    Module module = fileIndex.getModuleForFile(virtualFile);
    if (module == null) {
        final VirtualFileSystem fs = virtualFile.getFileSystem();
        if (fs instanceof JarFileSystem) {
            final VirtualFile jar = ((JarFileSystem) fs).getVirtualFileForJar(virtualFile);
            if (jar != null) {
                module = fileIndex.getModuleForFile(jar);
            }
        }
    }
    if (module != null) {
        String[] javadocPaths = JavaModuleExternalPaths.getInstance(module).getJavadocUrls();
        final List<String> httpRoots = PlatformDocumentationUtil.getHttpRoots(javadocPaths, relPath);
        // if found nothing and the file is from library classes, fall back to order entries
        if (httpRoots != null || !fileIndex.isInLibraryClasses(virtualFile)) {
            return httpRoots;
        }
    }
    for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) {
        for (VirtualFile root : orderEntry.getFiles(JavadocOrderRootType.getInstance())) {
            if (root.getFileSystem() == JarFileSystem.getInstance()) {
                VirtualFile file = root.findFileByRelativePath(relPath);
                List<Url> urls = file == null ? null : BuiltInWebBrowserUrlProviderKt.getBuiltInServerUrls(file, project, null);
                if (!ContainerUtil.isEmpty(urls)) {
                    List<String> result = new SmartList<>();
                    for (Url url : urls) {
                        result.add(url.toExternalForm());
                    }
                    return result;
                }
            }
        }
        List<String> httpRoot = PlatformDocumentationUtil.getHttpRoots(JavadocOrderRootType.getUrls(orderEntry), relPath);
        if (httpRoot != null) {
            return httpRoot;
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileSystem(com.intellij.openapi.vfs.VirtualFileSystem) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Module(com.intellij.openapi.module.Module) SmartList(com.intellij.util.SmartList) Url(com.intellij.util.Url) Nullable(org.jetbrains.annotations.Nullable)

Example 88 with SmartList

use of com.intellij.util.SmartList in project intellij-community by JetBrains.

the class StatementParser method parseCodeBlock.

@Nullable
public PsiBuilder.Marker parseCodeBlock(final PsiBuilder builder, final boolean isStatement) {
    if (builder.getTokenType() != JavaTokenType.LBRACE)
        return null;
    else if (isStatement && isParseStatementCodeBlocksDeep(builder))
        return parseCodeBlockDeep(builder, false);
    final PsiBuilder.Marker codeBlock = builder.mark();
    builder.advanceLexer();
    boolean greedyBlock = false;
    int braceCount = 1;
    while (true) {
        final IElementType tokenType = builder.getTokenType();
        if (tokenType == null) {
            greedyBlock = true;
            break;
        }
        if (tokenType == JavaTokenType.LBRACE) {
            braceCount++;
        } else if (tokenType == JavaTokenType.RBRACE) {
            braceCount--;
        }
        builder.advanceLexer();
        if (braceCount == 0) {
            break;
        } else if (braceCount == 1 && (tokenType == JavaTokenType.SEMICOLON || tokenType == JavaTokenType.RBRACE)) {
            final PsiBuilder.Marker position = builder.mark();
            final List<IElementType> list = new SmartList<>();
            while (true) {
                final IElementType type = builder.getTokenType();
                if (ElementType.PRIMITIVE_TYPE_BIT_SET.contains(type) || ElementType.MODIFIER_BIT_SET.contains(type) || (type == JavaTokenType.IDENTIFIER && list.size() > 0) || type == JavaTokenType.LT || type == JavaTokenType.GT || type == JavaTokenType.GTGT || type == JavaTokenType.GTGTGT || type == JavaTokenType.COMMA || type == JavaTokenType.DOT || type == JavaTokenType.EXTENDS_KEYWORD || type == JavaTokenType.IMPLEMENTS_KEYWORD) {
                    list.add(type);
                    builder.advanceLexer();
                } else {
                    break;
                }
            }
            if (builder.getTokenType() == JavaTokenType.LPARENTH && list.size() >= 2) {
                final IElementType last = list.get(list.size() - 1);
                final IElementType prevLast = list.get(list.size() - 2);
                if (last == JavaTokenType.IDENTIFIER && (prevLast == JavaTokenType.IDENTIFIER || ElementType.PRIMITIVE_TYPE_BIT_SET.contains(prevLast))) {
                    position.rollbackTo();
                    greedyBlock = true;
                    break;
                }
            }
            position.drop();
        }
    }
    codeBlock.collapse(JavaElementType.CODE_BLOCK);
    if (greedyBlock) {
        codeBlock.setCustomEdgeTokenBinders(null, WhitespacesBinders.GREEDY_RIGHT_BINDER);
    }
    return codeBlock;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) List(java.util.List) SmartList(com.intellij.util.SmartList) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with SmartList

use of com.intellij.util.SmartList in project intellij-community by JetBrains.

the class AbstractLayoutCodeProcessor method getSelectedRanges.

protected static List<TextRange> getSelectedRanges(@NotNull SelectionModel selectionModel) {
    final List<TextRange> ranges = new SmartList<>();
    if (selectionModel.hasSelection()) {
        TextRange range = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
        ranges.add(range);
    }
    return ranges;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) SmartList(com.intellij.util.SmartList)

Example 90 with SmartList

use of com.intellij.util.SmartList in project intellij-community by JetBrains.

the class ExecutionManagerImpl method restartRunProfile.

@Override
public void restartRunProfile(@NotNull final ExecutionEnvironment environment) {
    RunnerAndConfigurationSettings configuration = environment.getRunnerAndConfigurationSettings();
    List<RunContentDescriptor> runningIncompatible;
    if (configuration == null) {
        runningIncompatible = Collections.emptyList();
    } else {
        runningIncompatible = getIncompatibleRunningDescriptors(configuration);
    }
    RunContentDescriptor contentToReuse = environment.getContentToReuse();
    final List<RunContentDescriptor> runningOfTheSameType = new SmartList<>();
    if (configuration != null && configuration.isSingleton()) {
        runningOfTheSameType.addAll(getRunningDescriptorsOfTheSameConfigType(configuration));
    } else if (isProcessRunning(contentToReuse)) {
        runningOfTheSameType.add(contentToReuse);
    }
    List<RunContentDescriptor> runningToStop = ContainerUtil.concat(runningOfTheSameType, runningIncompatible);
    if (!runningToStop.isEmpty()) {
        if (configuration != null) {
            if (!runningOfTheSameType.isEmpty() && (runningOfTheSameType.size() > 1 || contentToReuse == null || runningOfTheSameType.get(0) != contentToReuse) && !userApprovesStopForSameTypeConfigurations(environment.getProject(), configuration.getName(), runningOfTheSameType.size())) {
                return;
            }
            if (!runningIncompatible.isEmpty() && !userApprovesStopForIncompatibleConfigurations(myProject, configuration.getName(), runningIncompatible)) {
                return;
            }
        }
        for (RunContentDescriptor descriptor : runningToStop) {
            stopProcess(descriptor);
        }
    }
    if (myAwaitingRunProfiles.get(environment.getRunProfile()) == environment) {
        // defense from rerunning exactly the same ExecutionEnvironment
        return;
    }
    myAwaitingRunProfiles.put(environment.getRunProfile(), environment);
    awaitTermination(new Runnable() {

        @Override
        public void run() {
            if (myAwaitingRunProfiles.get(environment.getRunProfile()) != environment) {
                // a new rerun has been requested before starting this one, ignore this rerun
                return;
            }
            if ((DumbService.getInstance(myProject).isDumb() && !Registry.is("dumb.aware.run.configurations")) || ExecutorRegistry.getInstance().isStarting(environment)) {
                awaitTermination(this, 100);
                return;
            }
            for (RunContentDescriptor descriptor : runningOfTheSameType) {
                ProcessHandler processHandler = descriptor.getProcessHandler();
                if (processHandler != null && !processHandler.isProcessTerminated()) {
                    awaitTermination(this, 100);
                    return;
                }
            }
            myAwaitingRunProfiles.remove(environment.getRunProfile());
            start(environment);
        }
    }, 50);
}
Also used : RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessHandler(com.intellij.execution.process.ProcessHandler) SmartList(com.intellij.util.SmartList)

Aggregations

SmartList (com.intellij.util.SmartList)163 NotNull (org.jetbrains.annotations.NotNull)70 Nullable (org.jetbrains.annotations.Nullable)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)24 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)14 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 List (java.util.List)12 Element (org.jdom.Element)12 File (java.io.File)11 THashSet (gnu.trove.THashSet)9 ContainerUtil (com.intellij.util.containers.ContainerUtil)8 ArrayList (java.util.ArrayList)8 Collection (java.util.Collection)8 Pair (com.intellij.openapi.util.Pair)7 PsiFile (com.intellij.psi.PsiFile)6 IOException (java.io.IOException)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)5 IElementType (com.intellij.psi.tree.IElementType)5