Search in sources :

Example 21 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-plugins by JetBrains.

the class DartExtractLocalVariableRefactoringTest method createRefactoring.

@NotNull
private ServerExtractLocalVariableRefactoring createRefactoring(String filePath) {
    ((CodeInsightTestFixtureImpl) myFixture).canChangeDocumentDuringHighlighting(true);
    final PsiFile psiFile = myFixture.configureByFile(filePath);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            VfsUtil.saveText(psiFile.getVirtualFile(), StringUtil.convertLineSeparators(psiFile.getText(), "\r\n"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    // make sure server is warmed up
    myFixture.doHighlighting();
    // find the Element to rename
    final SelectionModel selectionModel = getEditor().getSelectionModel();
    int offset = selectionModel.getSelectionStart();
    final int length = selectionModel.getSelectionEnd() - offset;
    return new ServerExtractLocalVariableRefactoring(getProject(), psiFile.getVirtualFile(), offset, length);
}
Also used : ServerExtractLocalVariableRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring) CodeInsightTestFixtureImpl(com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class GithubOpenInBrowserAction method makeUrlToOpen.

@Nullable
private static String makeUrlToOpen(@Nullable Editor editor, @NotNull String relativePath, @NotNull String branch, @NotNull String githubRemoteUrl) {
    StringBuilder builder = new StringBuilder();
    String githubRepoUrl = GithubUrlUtil.makeGithubRepoUrlFromRemoteUrl(githubRemoteUrl);
    if (githubRepoUrl == null)
        return null;
    if (StringUtil.isEmptyOrSpaces(relativePath)) {
        builder.append(githubRepoUrl).append("/tree/").append(branch);
    } else {
        builder.append(githubRepoUrl).append("/blob/").append(branch).append('/').append(relativePath);
    }
    if (editor != null && editor.getDocument().getLineCount() >= 1) {
        // lines are counted internally from 0, but from 1 on github
        SelectionModel selectionModel = editor.getSelectionModel();
        final int begin = editor.getDocument().getLineNumber(selectionModel.getSelectionStart()) + 1;
        final int selectionEnd = selectionModel.getSelectionEnd();
        int end = editor.getDocument().getLineNumber(selectionEnd) + 1;
        if (editor.getDocument().getLineStartOffset(end - 1) == selectionEnd) {
            end -= 1;
        }
        builder.append("#L").append(begin).append("-L").append(end);
    }
    return builder.toString();
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class HighlightUsagesHandler method invoke.

public static void invoke(@NotNull final Project project, @NotNull final Editor editor, final PsiFile file) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (file == null && !selectionModel.hasSelection()) {
        selectionModel.selectWordAtCaret(false);
    }
    if (file == null || selectionModel.hasSelection()) {
        doRangeHighlighting(editor, project);
        return;
    }
    final HighlightUsagesHandlerBase handler = createCustomHandler(editor, file);
    if (handler != null) {
        final String featureId = handler.getFeatureId();
        if (featureId != null) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(featureId);
        }
        handler.highlightUsages();
        return;
    }
    DumbService.getInstance(project).withAlternativeResolveEnabled(() -> {
        UsageTarget[] usageTargets = getUsageTargets(editor, file);
        if (usageTargets == null) {
            handleNoUsageTargets(file, editor, selectionModel, project);
            return;
        }
        boolean clearHighlights = isClearHighlights(editor);
        for (UsageTarget target : usageTargets) {
            target.highlightUsages(file, editor, clearHighlights);
        }
    });
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) UsageTarget(com.intellij.usages.UsageTarget)

Example 24 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class VariableInplaceRenamer method beforeTemplateStart.

@Override
protected void beforeTemplateStart() {
    super.beforeTemplateStart();
    myLanguage = myScope.getLanguage();
    if (shouldCreateSnapshot()) {
        final ResolveSnapshotProvider resolveSnapshotProvider = INSTANCE.forLanguage(myLanguage);
        mySnapshot = resolveSnapshotProvider != null ? resolveSnapshotProvider.createSnapshot(myScope) : null;
    }
    final SelectionModel selectionModel = myEditor.getSelectionModel();
    mySelectedRange = selectionModel.hasSelection() ? new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()) : null;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange)

Example 25 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class LivePreviewController method performReplaceAll.

public void performReplaceAll(Editor e) {
    Project project = mySearchResults.getProject();
    if (!ReadonlyStatusHandler.ensureDocumentWritable(project, e.getDocument()))
        return;
    if (mySearchResults.getFindModel() != null) {
        final FindModel copy = new FindModel();
        copy.copyFrom(mySearchResults.getFindModel());
        final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel();
        final int offset;
        if (!selectionModel.hasSelection() || copy.isGlobal()) {
            copy.setGlobal(true);
            offset = 0;
        } else {
            offset = selectionModel.getBlockSelectionStarts()[0];
        }
        FindUtil.replace(project, e, offset, copy, this);
    }
}
Also used : Project(com.intellij.openapi.project.Project) SelectionModel(com.intellij.openapi.editor.SelectionModel)

Aggregations

SelectionModel (com.intellij.openapi.editor.SelectionModel)76 TextRange (com.intellij.openapi.util.TextRange)21 Document (com.intellij.openapi.editor.Document)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)16 Editor (com.intellij.openapi.editor.Editor)14 Nullable (org.jetbrains.annotations.Nullable)11 CaretModel (com.intellij.openapi.editor.CaretModel)10 PsiFile (com.intellij.psi.PsiFile)8 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)3 Pass (com.intellij.openapi.util.Pass)3 List (java.util.List)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2