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);
}
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());
}
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;
}
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();
}
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);
}
}
Aggregations