Search in sources :

Example 11 with SelectionModel

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

the class GroovyExtractChooser method invoke.

public static InitialInfo invoke(Project project, Editor editor, PsiFile file, int start, int end, boolean forceStatements) throws GrRefactoringError {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (!(file instanceof GroovyFileBase)) {
        throw new GrRefactoringError(GroovyRefactoringBundle.message("only.in.groovy.files"));
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) {
        throw new GrRefactoringError(RefactoringBundle.message("readonly.occurences.found"));
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final StringPartInfo stringPart = StringPartInfo.findStringPart(file, start, end);
    if (stringPart != null) {
        return new InitialInfo(new VariableInfo[0], new VariableInfo[0], PsiElement.EMPTY_ARRAY, GrStatement.EMPTY_ARRAY, new ArrayList<>(), stringPart, project, null);
    }
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!forceStatements) {
        GrVariable variable = GrIntroduceHandlerBase.findVariable(file, start, end);
        if (variable != null) {
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                TextRange range = initializer.getTextRange();
                return buildInfo(project, file, range.getStartOffset(), range.getEndOffset(), forceStatements, selectionModel, variable);
            }
        }
    }
    return buildInfo(project, file, start, end, forceStatements, selectionModel, null);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

Example 12 with SelectionModel

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

the class GrIntroduceParameterHandler method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) {
    if (editor == null || file == null)
        return;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        final int offset = editor.getCaretModel().getOffset();
        final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, offset, false);
        if (expressions.isEmpty()) {
            GrIntroduceHandlerBase.updateSelectionForVariable(editor, file, selectionModel, offset);
        } else if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
            final TextRange textRange = expressions.get(0).getTextRange();
            selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
        } else {
            IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {

                @Override
                public void pass(final GrExpression selectedValue) {
                    invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset());
                }
            }, grExpression -> grExpression.getText());
            return;
        }
    }
    invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
Also used : GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) HelpID(org.jetbrains.plugins.groovy.refactoring.HelpID) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) DataContext(com.intellij.openapi.actionSystem.DataContext) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) GrIntroduceVariableHandler(org.jetbrains.plugins.groovy.refactoring.introduce.variable.GrIntroduceVariableHandler) MethodOrClosureScopeChooser(org.jetbrains.plugins.groovy.refactoring.ui.MethodOrClosureScopeChooser) ArrayList(java.util.ArrayList) InitialInfo(org.jetbrains.plugins.groovy.refactoring.extract.InitialInfo) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) IntroduceOccurrencesChooser(org.jetbrains.plugins.groovy.refactoring.introduce.IntroduceOccurrencesChooser) GrIntroduceHandlerBase(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) IntroduceTargetChooser(com.intellij.refactoring.IntroduceTargetChooser) GrIntroduceContext(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContext) PsiMethod(com.intellij.psi.PsiMethod) TextRange(com.intellij.openapi.util.TextRange) SuperMethodWarningUtil(com.intellij.ide.util.SuperMethodWarningUtil) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Pass(com.intellij.openapi.util.Pass) OccurrencesChooser(com.intellij.refactoring.introduce.inplace.OccurrencesChooser) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) NotNull(org.jetbrains.annotations.NotNull) GroovyExtractChooser(org.jetbrains.plugins.groovy.refactoring.extract.GroovyExtractChooser) Pass(com.intellij.openapi.util.Pass) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 13 with SelectionModel

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

the class CCAddAnswerPlaceholder method canAddPlaceholder.

private static boolean canAddPlaceholder(@NotNull CCState state) {
    Editor editor = state.getEditor();
    SelectionModel selectionModel = editor.getSelectionModel();
    TaskFile taskFile = state.getTaskFile();
    if (selectionModel.hasSelection()) {
        int start = selectionModel.getSelectionStart();
        int end = selectionModel.getSelectionEnd();
        return !arePlaceholdersIntersect(taskFile, start, end);
    }
    int offset = editor.getCaretModel().getOffset();
    return StudyUtils.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders()) == null;
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor)

Example 14 with SelectionModel

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

the class SearchDialog method show.

@Override
public void show() {
    StructuralSearchPlugin.getInstance(getProject()).setDialogVisible(true);
    if (!useLastConfiguration) {
        final Editor editor = searchContext.getEditor();
        boolean setSomeText = false;
        if (editor != null) {
            final SelectionModel selectionModel = editor.getSelectionModel();
            if (selectionModel.hasSelection()) {
                addOrReplaceSelection(selectionModel.getSelectedText());
                existingTemplatesComponent.getPatternTree().setSelectionPath(null);
                existingTemplatesComponent.getHistoryList().setSelectedIndex(-1);
                setSomeText = true;
            }
        }
        if (!setSomeText) {
            int selection = existingTemplatesComponent.getHistoryList().getSelectedIndex();
            if (selection != -1) {
                setValuesFromConfig((Configuration) existingTemplatesComponent.getHistoryList().getSelectedValue());
            }
        }
    }
    initiateValidation();
    super.show();
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor)

Example 15 with SelectionModel

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

the class JsonBySchemaObjectCompletionContributor method insertPropertyWithEnum.

public static void insertPropertyWithEnum(InsertionContext context, Editor editor, String defaultValue, List<Object> values, JsonSchemaType type, String comma) {
    final boolean isNumber = type != null && (JsonSchemaType._integer.equals(type) || JsonSchemaType._number.equals(type)) || type == null && (defaultValue != null && !StringUtil.isQuotedString(defaultValue) || values != null && ContainerUtil.and(values, v -> !(v instanceof String)));
    boolean hasValues = !ContainerUtil.isEmpty(values);
    boolean hasDefaultValue = !StringUtil.isEmpty(defaultValue);
    String stringToInsert = ":" + (hasDefaultValue ? defaultValue : (isNumber ? "" : "\"\"")) + comma;
    EditorModificationUtil.insertStringAtCaret(editor, stringToInsert, false, true, 1);
    if (!isNumber || hasDefaultValue) {
        SelectionModel model = editor.getSelectionModel();
        int caretStart = model.getSelectionStart();
        int newOffset = caretStart + (hasDefaultValue ? defaultValue.length() : 1);
        if (hasDefaultValue && !isNumber)
            newOffset--;
        model.setSelection(isNumber ? caretStart : (caretStart + 1), newOffset);
        editor.getCaretModel().moveToOffset(newOffset);
    }
    formatInsertedString(context, stringToInsert.length());
    if (hasValues) {
        AutoPopupController.getInstance(context.getProject()).autoPopupMemberLookup(context.getEditor(), null);
    }
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContainerUtil(com.intellij.util.containers.ContainerUtil) IdeActions(com.intellij.openapi.actionSystem.IdeActions) ArrayList(java.util.ArrayList) SchemaType(com.jetbrains.jsonSchema.extension.SchemaType) AutoPopupController(com.intellij.codeInsight.AutoPopupController) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) DataManager(com.intellij.ide.DataManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) EditorModificationUtil(com.intellij.openapi.editor.EditorModificationUtil) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) Editor(com.intellij.openapi.editor.Editor) com.intellij.codeInsight.completion(com.intellij.codeInsight.completion) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) List(java.util.List) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) JsonStringLiteral(com.intellij.json.psi.JsonStringLiteral) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler) Consumer(com.intellij.util.Consumer) UsageTrigger(com.intellij.internal.statistic.UsageTrigger) 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