Search in sources :

Example 11 with DartAnalysisServerService

use of com.jetbrains.lang.dart.analyzer.DartAnalysisServerService in project intellij-plugins by JetBrains.

the class DartStyleAction method runOverEditor.

protected void runOverEditor(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile) {
    final Document document = editor.getDocument();
    if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document))
        return;
    final int caretOffset = editor.getCaretModel().getOffset();
    final int lineLength = getRightMargin(project);
    final DartAnalysisServerService das = DartAnalysisServerService.getInstance(project);
    das.updateFilesContent();
    DartAnalysisServerService.FormatResult formatResult = das.edit_format(psiFile.getVirtualFile(), caretOffset, 0, lineLength);
    if (formatResult == null) {
        showHintLater(editor, DartBundle.message("dart.style.hint.failed"), true);
        LOG.warn("Unexpected response from edit_format, formatResult is null");
        return;
    }
    final Runnable runnable = () -> {
        final List<SourceEdit> edits = formatResult.getEdits();
        if (edits == null || edits.size() == 0) {
            showHintLater(editor, DartBundle.message("dart.style.hint.already.good"), false);
        } else if (edits.size() == 1) {
            final String replacement = StringUtil.convertLineSeparators(edits.get(0).getReplacement());
            document.replaceString(0, document.getTextLength(), replacement);
            final int offset = das.getConvertedOffset(psiFile.getVirtualFile(), formatResult.getOffset());
            editor.getCaretModel().moveToOffset(offset);
            showHintLater(editor, DartBundle.message("dart.style.hint.success"), false);
        } else {
            showHintLater(editor, DartBundle.message("dart.style.hint.failed"), true);
            LOG.warn("Unexpected response from edit_format, formatResult.getEdits().size() = " + edits.size());
        }
    };
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, runnable, DartBundle.message("dart.style.action.name"), null));
}
Also used : List(java.util.List) Document(com.intellij.openapi.editor.Document) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)

Example 12 with DartAnalysisServerService

use of com.jetbrains.lang.dart.analyzer.DartAnalysisServerService in project intellij-plugins by JetBrains.

the class DartAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
    if (holder.isBatchMode())
        return;
    final AnnotationSession session = holder.getCurrentAnnotationSession();
    if (session.getUserData(DART_SERVER_DATA_HANDLED) != Boolean.TRUE) {
        session.putUserData(DART_SERVER_DATA_HANDLED, Boolean.TRUE);
        final VirtualFile vFile = element.getContainingFile().getVirtualFile();
        final DartAnalysisServerService service = DartAnalysisServerService.getInstance(element.getProject());
        if (canBeAnalyzedByServer(element.getProject(), vFile) && service.serverReadyForRequest(element.getProject())) {
            service.updateFilesContent();
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                service.waitForAnalysisToComplete_TESTS_ONLY(vFile);
            }
            applyServerHighlighting(vFile, holder);
        }
    }
    if (DartTokenTypes.COLON == element.getNode().getElementType() && element.getParent() instanceof DartTernaryExpression) {
        holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.OPERATION_SIGN);
        return;
    }
    if (DartTokenTypesSets.BUILT_IN_IDENTIFIERS.contains(element.getNode().getElementType())) {
        if (element.getNode().getTreeParent().getElementType() != DartTokenTypes.ID) {
            holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.KEYWORD);
            return;
        }
    }
    // sync*, async* and yield*
    if (DartTokenTypes.MUL == element.getNode().getElementType()) {
        final ASTNode previous = element.getNode().getTreePrev();
        if (previous != null && (previous.getElementType() == DartTokenTypes.SYNC || previous.getElementType() == DartTokenTypes.ASYNC || previous.getElementType() == DartTokenTypes.YIELD)) {
            holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.KEYWORD);
        }
    }
    if (element.getNode().getElementType() == DartTokenTypes.REGULAR_STRING_PART) {
        highlightEscapeSequences(element, holder);
        return;
    }
    if (element instanceof DartSymbolLiteralExpression) {
        holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.SYMBOL_LITERAL);
        //noinspection UnnecessaryReturnStatement
        return;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AnnotationSession(com.intellij.lang.annotation.AnnotationSession) DartSymbolLiteralExpression(com.jetbrains.lang.dart.psi.DartSymbolLiteralExpression) ASTNode(com.intellij.lang.ASTNode) DartTernaryExpression(com.jetbrains.lang.dart.psi.DartTernaryExpression) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)

Example 13 with DartAnalysisServerService

use of com.jetbrains.lang.dart.analyzer.DartAnalysisServerService in project intellij-plugins by JetBrains.

the class DartAnnotator method applyServerHighlighting.

private static void applyServerHighlighting(@NotNull final VirtualFile file, @NotNull final AnnotationHolder holder) {
    final PsiFile psiFile = holder.getCurrentAnnotationSession().getFile();
    final DartAnalysisServerService das = DartAnalysisServerService.getInstance(psiFile.getProject());
    for (DartServerData.DartError error : das.getErrors(file)) {
        if (shouldIgnoreMessageFromDartAnalyzer(file.getPath(), error.getAnalysisErrorFileSD()))
            continue;
        final Annotation annotation = createAnnotation(holder, error, psiFile.getTextLength());
        if (annotation != null) {
            final DartQuickFixSet quickFixSet = new DartQuickFixSet(psiFile.getManager(), file, error.getOffset(), error.getCode(), error.getSeverity());
            for (IntentionAction quickFix : quickFixSet.getQuickFixes()) {
                annotation.registerFix(quickFix);
            }
            if (error.getCode() != null) {
                annotation.setProblemGroup(new DartProblemGroup(error.getCode(), error.getSeverity()));
            }
        }
    }
    for (DartServerData.DartHighlightRegion region : das.getHighlight(file)) {
        final String attributeKey = HIGHLIGHTING_TYPE_MAP.get(region.getType());
        if (attributeKey != null) {
            final TextRange textRange = new TextRange(region.getOffset(), region.getOffset() + region.getLength());
            holder.createInfoAnnotation(textRange, null).setTextAttributes(TextAttributesKey.find(attributeKey));
        }
    }
}
Also used : DartQuickFixSet(com.jetbrains.lang.dart.fixes.DartQuickFixSet) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) DartServerData(com.jetbrains.lang.dart.analyzer.DartServerData) Annotation(com.intellij.lang.annotation.Annotation)

Example 14 with DartAnalysisServerService

use of com.jetbrains.lang.dart.analyzer.DartAnalysisServerService in project intellij-plugins by JetBrains.

the class DartSortMembersAction method runOverEditor.

protected void runOverEditor(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile) {
    final Document document = editor.getDocument();
    if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document))
        return;
    final String path = psiFile.getVirtualFile().getPath();
    final DartAnalysisServerService service = DartAnalysisServerService.getInstance(project);
    service.updateFilesContent();
    final SourceFileEdit fileEdit = service.edit_sortMembers(path);
    if (fileEdit == null) {
        showHintLater(editor, DartBundle.message("dart.sort.members.hint.failed"), true);
        LOG.warn("Unexpected response from edit_sortMembers, fileEdit is null");
        return;
    }
    final List<SourceEdit> edits = fileEdit.getEdits();
    if (edits == null || edits.size() == 0) {
        showHintLater(editor, DartBundle.message("dart.sort.members.hint.already.good"), false);
        return;
    }
    final Runnable runnable = () -> {
        AssistUtils.applySourceEdits(project, psiFile.getVirtualFile(), document, edits, Collections.emptySet());
        showHintLater(editor, DartBundle.message("dart.sort.members.hint.success"), false);
    };
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, runnable, DartBundle.message("dart.sort.members.action.name"), null));
}
Also used : SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) Document(com.intellij.openapi.editor.Document) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) SourceEdit(org.dartlang.analysis.server.protocol.SourceEdit)

Example 15 with DartAnalysisServerService

use of com.jetbrains.lang.dart.analyzer.DartAnalysisServerService in project intellij-plugins by JetBrains.

the class DartInheritorsSearcher method processQuery.

@Override
public void processQuery(@NotNull final DefinitionsScopedSearch.SearchParameters parameters, @NotNull final Processor<PsiElement> consumer) {
    final Ref<VirtualFile> fileRef = Ref.create();
    final Ref<Integer> offsetRef = Ref.create();
    final Ref<DartComponentType> componentTypeRef = Ref.create();
    prepare(parameters, fileRef, offsetRef, componentTypeRef);
    if (fileRef.isNull() || offsetRef.isNull() || componentTypeRef.isNull())
        return;
    ApplicationManager.getApplication().runReadAction(() -> {
        final List<TypeHierarchyItem> hierarchyItems = CachedValuesManager.getCachedValue(parameters.getElement(), () -> {
            final DartAnalysisServerService das = DartAnalysisServerService.getInstance(parameters.getElement().getProject());
            final List<TypeHierarchyItem> items = das.search_getTypeHierarchy(fileRef.get(), offsetRef.get(), false);
            return new CachedValueProvider.Result<>(items, PsiModificationTracker.MODIFICATION_COUNT);
        });
        final List<DartComponent> components = componentTypeRef.get() == DartComponentType.CLASS ? getSubClasses(parameters.getElement().getProject(), parameters.getScope(), hierarchyItems) : getSubMembers(parameters.getElement().getProject(), parameters.getScope(), hierarchyItems);
        for (DartComponent component : components) {
            consumer.process(component);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartComponent(com.jetbrains.lang.dart.psi.DartComponent) DartComponentType(com.jetbrains.lang.dart.DartComponentType) TypeHierarchyItem(org.dartlang.analysis.server.protocol.TypeHierarchyItem) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)

Aggregations

DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)22 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiFile (com.intellij.psi.PsiFile)3 Document (com.intellij.openapi.editor.Document)2 PsiElement (com.intellij.psi.PsiElement)2 UsageInfo (com.intellij.usageView.UsageInfo)2 DartServerData (com.jetbrains.lang.dart.analyzer.DartServerData)2 SourceChange (org.dartlang.analysis.server.protocol.SourceChange)2 SourceEdit (org.dartlang.analysis.server.protocol.SourceEdit)2 SourceFileEdit (org.dartlang.analysis.server.protocol.SourceFileEdit)2 GetServerPortConsumer (com.google.dart.server.GetServerPortConsumer)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 ASTNode (com.intellij.lang.ASTNode)1 Annotation (com.intellij.lang.annotation.Annotation)1 AnnotationSession (com.intellij.lang.annotation.AnnotationSession)1 Notification (com.intellij.notification.Notification)1 Caret (com.intellij.openapi.editor.Caret)1