use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class GrIntroduceHandlerBase method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final int offset = editor.getCaretModel().getOffset();
final List<GrExpression> expressions = collectExpressions(file, editor, offset, false);
if (expressions.isEmpty()) {
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());
}
}, GR_EXPRESSION_RENDERER);
return;
}
}
invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class I18nizeQuickFix method checkApplicability.
@Override
public void checkApplicability(final PsiFile psiFile, final Editor editor) throws IncorrectOperationException {
PsiLiteralExpression literalExpression = I18nizeAction.getEnclosingStringLiteral(psiFile, editor);
if (literalExpression != null) {
SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection())
return;
int start = selectionModel.getSelectionStart();
int end = selectionModel.getSelectionEnd();
TextRange textRange = literalExpression.getTextRange();
if (textRange.contains(start) && textRange.contains(end)) {
mySelectionRange = new TextRange(start, end);
return;
}
}
String message = CodeInsightBundle.message("i18nize.error.message");
throw new IncorrectOperationException(message);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class Intention method findMatchingElement.
@Nullable
PsiElement findMatchingElement(PsiFile file, Editor editor) {
if (!file.getViewProvider().getLanguages().contains(GroovyLanguage.INSTANCE)) {
return null;
}
SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
int start = selectionModel.getSelectionStart();
int end = selectionModel.getSelectionEnd();
if (0 <= start && start <= end) {
TextRange selectionRange = new TextRange(start, end);
PsiElement element = PsiImplUtil.findElementInRange(file, start, end, PsiElement.class);
while (element != null && element.getTextRange() != null && selectionRange.contains(element.getTextRange())) {
if (predicate.satisfiedBy(element))
return element;
element = element.getParent();
}
}
}
final int position = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(position);
while (element != null) {
if (predicate.satisfiedBy(element))
return element;
if (isStopElement(element))
break;
element = element.getParent();
}
element = file.findElementAt(position - 1);
while (element != null) {
if (predicate.satisfiedBy(element))
return element;
if (isStopElement(element))
return null;
element = element.getParent();
}
return null;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class ExtractMethodHandler method getElements.
public static PsiElement[] getElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
int startOffset = selectionModel.getSelectionStart();
int endOffset = selectionModel.getSelectionEnd();
PsiElement[] elements;
PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
if (expr != null) {
elements = new PsiElement[] { expr };
} else {
elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
if (elements.length == 0) {
final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset);
if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) {
final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression);
if (originalType != null) {
elements = new PsiElement[] { expression };
}
}
}
}
return elements;
}
final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, editor.getCaretModel().getOffset());
return expressions.toArray(new PsiElement[expressions.size()]);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class EscapeHandler method execute.
@Override
public void execute(Editor editor, DataContext dataContext) {
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null && !templateState.isFinished()) {
SelectionModel selectionModel = editor.getSelectionModel();
LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
// the idea behind lookup checking is that if there is a preselected value in lookup
// then user might want just to close lookup but not finish a template.
// E.g. user wants to move to the next template segment by Tab without completion invocation.
// If there is no selected value in completion that user definitely wants to finish template
boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
if (!selectionModel.hasSelection() && lookupIsEmpty) {
CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
templateState.gotoEnd(true);
return;
}
}
if (myOriginalHandler.isEnabled(editor, dataContext)) {
myOriginalHandler.execute(editor, dataContext);
}
}
Aggregations