use of com.intellij.openapi.editor.SelectionModel in project intellij-plugins by JetBrains.
the class DartExtractMethodRefactoringTest method createRefactoring.
@NotNull
private ServerExtractMethodRefactoring createRefactoring(String filePath) {
((CodeInsightTestFixtureImpl) myFixture).canChangeDocumentDuringHighlighting(true);
final PsiFile psiFile = myFixture.configureByFile(filePath);
// 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 ServerExtractMethodRefactoring(getProject(), psiFile.getVirtualFile(), offset, length);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PsiSelectionSearcher method searchElementsInSelection.
/**
* Searches elements in selection
*
* @param editor editor to get text selection
* @param project Project
* @param filter PsiElement filter, e.g. PsiMethodCallExpression.class
* @param searchChildrenOfFound if true, visitor will look for matching elements in the children of a found element, otherwise will not look inside found element.
* @param <T> type based on PsiElement type
* @return elements in selection
*/
@NotNull
public static <T extends PsiElement> List<T> searchElementsInSelection(Editor editor, Project project, final Class<T> filter, final boolean searchChildrenOfFound) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
return Collections.emptyList();
}
final TextRange selection = new UnfairTextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (file == null || file instanceof PsiCompiledElement) {
return Collections.emptyList();
}
final List<T> results = new ArrayList<>();
final PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (!selection.intersects(element.getTextRange())) {
return;
}
if (filter.isAssignableFrom(element.getClass())) {
results.add((T) element);
if (!searchChildrenOfFound) {
return;
}
}
super.visitElement(element);
}
};
file.accept(visitor);
return results;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class UnicodeUnescapeIntention method processIntention.
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
// does not check if Unicode escape is inside char or string literal (garbage in, garbage out)
final Document document = editor.getDocument();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final String text = document.getText(new TextRange(start, end));
final int textLength = end - start;
final StringBuilder replacement = new StringBuilder(textLength);
int anchor = 0;
while (true) {
final int index = indexOfUnicodeEscape(text, anchor + 1);
if (index < 0) {
break;
}
replacement.append(text.substring(anchor, index));
int hexStart = index + 1;
while (text.charAt(hexStart) == 'u') {
hexStart++;
}
anchor = hexStart + 4;
final int c = Integer.parseInt(text.substring(hexStart, anchor), 16);
replacement.appendCodePoint(c);
}
replacement.append(text.substring(anchor, textLength));
document.replaceString(start, end, replacement);
} else {
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
final int lineNumber = document.getLineNumber(caretModel.getOffset());
final int lineStartOffset = document.getLineStartOffset(lineNumber);
final String line = document.getText(new TextRange(lineStartOffset, document.getLineEndOffset(lineNumber)));
final int column = caretModel.getLogicalPosition().column;
final int index1 = indexOfUnicodeEscape(line, column);
final int index2 = indexOfUnicodeEscape(line, column + 1);
// if the caret is between two unicode escapes, replace the one to the right
final int escapeStart = index2 == column ? index2 : index1;
int hexStart = escapeStart + 1;
while (line.charAt(hexStart) == 'u') {
hexStart++;
}
final char c = (char) Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
if (Character.isHighSurrogate(c)) {
hexStart += 4;
if (line.charAt(hexStart++) == '\\' && line.charAt(hexStart++) == 'u') {
while (line.charAt(hexStart) == 'u') hexStart++;
final char d = (char) Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(new char[] { c, d }));
return;
}
} else if (Character.isLowSurrogate(c)) {
if (escapeStart >= 6 && StringUtil.isHexDigit(line.charAt(escapeStart - 1)) && StringUtil.isHexDigit(line.charAt(escapeStart - 2)) && StringUtil.isHexDigit(line.charAt(escapeStart - 3)) && StringUtil.isHexDigit(line.charAt(escapeStart - 4))) {
int i = escapeStart - 5;
while (i > 0 && line.charAt(i) == 'u') i--;
if (line.charAt(i) == '\\' && (i == 0 || line.charAt(i - 1) != '\\')) {
final char b = (char) Integer.parseInt(line.substring(escapeStart - 4, escapeStart), 16);
document.replaceString(lineStartOffset + i, lineStartOffset + hexStart + 4, String.valueOf(new char[] { b, c }));
return;
}
}
}
document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(c));
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PredefinedSearchScopeProviderImpl method getPredefinedScopes.
@NotNull
@Override
public List<SearchScope> getPredefinedScopes(@NotNull final Project project, @Nullable final DataContext dataContext, boolean suggestSearchInLibs, boolean prevSearchFiles, boolean currentSelection, boolean usageView, boolean showEmptyScopes) {
Collection<SearchScope> result = ContainerUtil.newLinkedHashSet();
result.add(GlobalSearchScope.everythingScope(project));
result.add(GlobalSearchScope.projectScope(project));
if (suggestSearchInLibs) {
result.add(GlobalSearchScope.allScope(project));
}
if (ModuleUtil.isSupportedRootType(project, JavaSourceRootType.TEST_SOURCE)) {
result.add(GlobalSearchScopesCore.projectProductionScope(project));
result.add(GlobalSearchScopesCore.projectTestScope(project));
}
result.add(ScratchFileServiceImpl.buildScratchesSearchScope());
final GlobalSearchScope openFilesScope = GlobalSearchScopes.openFilesScope(project);
if (openFilesScope != GlobalSearchScope.EMPTY_SCOPE) {
result.add(openFilesScope);
} else if (showEmptyScopes) {
result.add(new LocalSearchScope(PsiElement.EMPTY_ARRAY, IdeBundle.message("scope.open.files")));
}
final Editor selectedTextEditor = ApplicationManager.getApplication().isDispatchThread() ? FileEditorManager.getInstance(project).getSelectedTextEditor() : null;
PsiFile psiFile = selectedTextEditor == null ? null : PsiDocumentManager.getInstance(project).getPsiFile(selectedTextEditor.getDocument());
PsiFile currentFile = psiFile;
if (dataContext != null) {
PsiElement dataContextElement = CommonDataKeys.PSI_FILE.getData(dataContext);
if (dataContextElement == null) {
dataContextElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
}
if (dataContextElement == null && psiFile != null) {
dataContextElement = psiFile;
}
if (dataContextElement != null) {
if (!PlatformUtils.isCidr()) {
// TODO: have an API to disable module scopes.
Module module = ModuleUtilCore.findModuleForPsiElement(dataContextElement);
if (module == null) {
module = LangDataKeys.MODULE.getData(dataContext);
}
if (module != null && !(ModuleType.get(module) instanceof InternalModuleType)) {
result.add(module.getModuleScope());
}
}
if (currentFile == null) {
currentFile = dataContextElement.getContainingFile();
}
}
}
if (currentFile != null || showEmptyScopes) {
PsiElement[] scope = currentFile != null ? new PsiElement[] { currentFile } : PsiElement.EMPTY_ARRAY;
result.add(new LocalSearchScope(scope, IdeBundle.message("scope.current.file")));
}
if (currentSelection && selectedTextEditor != null && psiFile != null) {
SelectionModel selectionModel = selectedTextEditor.getSelectionModel();
if (selectionModel.hasSelection()) {
int start = selectionModel.getSelectionStart();
final PsiElement startElement = psiFile.findElementAt(start);
if (startElement != null) {
int end = selectionModel.getSelectionEnd();
final PsiElement endElement = psiFile.findElementAt(end);
if (endElement != null) {
final PsiElement parent = PsiTreeUtil.findCommonParent(startElement, endElement);
if (parent != null) {
final List<PsiElement> elements = new ArrayList<>();
final PsiElement[] children = parent.getChildren();
TextRange selection = new TextRange(start, end);
for (PsiElement child : children) {
if (!(child instanceof PsiWhiteSpace) && child.getContainingFile() != null && selection.contains(child.getTextOffset())) {
elements.add(child);
}
}
if (!elements.isEmpty()) {
SearchScope local = new LocalSearchScope(PsiUtilCore.toPsiElementArray(elements), IdeBundle.message("scope.selection"));
result.add(local);
}
}
}
}
}
}
if (usageView) {
addHierarchyScope(project, result);
UsageView selectedUsageView = UsageViewManager.getInstance(project).getSelectedUsageView();
if (selectedUsageView != null && !selectedUsageView.isSearchInProgress()) {
final Set<Usage> usages = ContainerUtil.newTroveSet(selectedUsageView.getUsages());
usages.removeAll(selectedUsageView.getExcludedUsages());
if (prevSearchFiles) {
final Set<VirtualFile> files = collectFiles(usages, true);
if (!files.isEmpty()) {
GlobalSearchScope prev = new GlobalSearchScope(project) {
private Set<VirtualFile> myFiles;
@NotNull
@Override
public String getDisplayName() {
return IdeBundle.message("scope.files.in.previous.search.result");
}
@Override
public synchronized boolean contains(@NotNull VirtualFile file) {
if (myFiles == null) {
myFiles = collectFiles(usages, false);
}
return myFiles.contains(file);
}
@Override
public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
return 0;
}
@Override
public boolean isSearchInModuleContent(@NotNull Module aModule) {
return true;
}
@Override
public boolean isSearchInLibraries() {
return true;
}
};
result.add(prev);
}
} else {
final List<PsiElement> results = new ArrayList<>(usages.size());
for (Usage usage : usages) {
if (usage instanceof PsiElementUsage) {
final PsiElement element = ((PsiElementUsage) usage).getElement();
if (element != null && element.isValid() && element.getContainingFile() != null) {
results.add(element);
}
}
}
if (!results.isEmpty()) {
result.add(new LocalSearchScope(PsiUtilCore.toPsiElementArray(results), IdeBundle.message("scope.previous.search.results")));
}
}
}
}
final FavoritesManager favoritesManager = FavoritesManager.getInstance(project);
if (favoritesManager != null) {
for (final String favorite : favoritesManager.getAvailableFavoritesListNames()) {
final Collection<TreeItem<Pair<AbstractUrl, String>>> rootUrls = favoritesManager.getFavoritesListRootUrls(favorite);
// ignore unused root
if (rootUrls.isEmpty())
continue;
result.add(new GlobalSearchScope(project) {
@NotNull
@Override
public String getDisplayName() {
return "Favorite \'" + favorite + "\'";
}
@Override
public boolean contains(@NotNull final VirtualFile file) {
return ReadAction.compute(() -> favoritesManager.contains(favorite, file));
}
@Override
public int compare(@NotNull final VirtualFile file1, @NotNull final VirtualFile file2) {
return 0;
}
@Override
public boolean isSearchInModuleContent(@NotNull final Module aModule) {
return true;
}
@Override
public boolean isSearchInLibraries() {
return true;
}
});
}
}
ContainerUtil.addIfNotNull(result, getSelectedFilesScope(project, dataContext));
return ContainerUtil.newArrayList(result);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class XQuickEvaluateHandler method getExpressionInfo.
@NotNull
private static Promise<ExpressionInfo> getExpressionInfo(final XDebuggerEvaluator evaluator, final Project project, final ValueHintType type, @NotNull Editor editor, final int offset) {
SelectionModel selectionModel = editor.getSelectionModel();
int selectionStart = selectionModel.getSelectionStart();
int selectionEnd = selectionModel.getSelectionEnd();
if ((type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT) && selectionModel.hasSelection() && selectionStart <= offset && offset <= selectionEnd) {
return Promise.resolve(new ExpressionInfo(new TextRange(selectionStart, selectionEnd)));
}
return evaluator.getExpressionInfoAtOffsetAsync(project, editor.getDocument(), offset, type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT);
}
Aggregations