use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class PyStringLiteralTest method testEscaperOffsetInLongUnicodeEscape.
public void testEscaperOffsetInLongUnicodeEscape() {
final PyStringLiteralExpression expr = createLiteralFromText("u'XXX a\\U0001F600\\U0001F600b YYY'");
final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
final TextRange range = TextRange.create(6, 28);
assertEquals(6, escaper.getOffsetInHost(0, range));
assertEquals(7, escaper.getOffsetInHost(1, range));
// Each \\U0001F600 is represented as a surrogate pair, hence 2 characters-wide step in decoded text
assertEquals(17, escaper.getOffsetInHost(3, range));
assertEquals(27, escaper.getOffsetInHost(5, range));
assertEquals(28, escaper.getOffsetInHost(6, range));
assertEquals(-1, escaper.getOffsetInHost(7, range));
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class CreateLocalVariableFromUsageFix method positionCursor.
@Nullable
protected static Editor positionCursor(Project project, PsiFile targetFile, PsiElement element) {
TextRange range = element.getTextRange();
int textOffset = range.getStartOffset();
VirtualFile vFile = targetFile.getVirtualFile();
assert vFile != null;
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile, textOffset);
return FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class RemoveUnnecessaryEscapeCharactersIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final Document document = editor.getDocument();
final TextRange range = element.getTextRange();
document.replaceString(range.getStartOffset(), range.getEndOffset(), removeUnnecessaryEscapeSymbols((GrLiteral) element));
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class BaseIntroduceAction method extractFromExpression.
private void extractFromExpression(Editor e, final XPathExpression expression) {
final Editor editor = (e instanceof EditorWindow) ? ((EditorWindow) e).getDelegate() : e;
final HighlightManager highlightManager = HighlightManager.getInstance(expression.getProject());
final Set<XPathExpression> matchingExpressions = RefactoringUtil.collectMatchingExpressions(expression);
final List<XmlTag> otherMatches = new ArrayList<>(matchingExpressions.size());
final ArrayList<RangeHighlighter> highlighters = new ArrayList<>(matchingExpressions.size() + 1);
if (matchingExpressions.size() > 0) {
final SelectionModel selectionModel = editor.getSelectionModel();
highlightManager.addRangeHighlight(editor, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
for (XPathExpression expr : matchingExpressions) {
final TextRange range = XsltCodeInsightUtil.getRangeInsideHostingFile(expr);
highlightManager.addRangeHighlight(editor, range.getStartOffset(), range.getEndOffset(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
final XmlTag tag = PsiTreeUtil.getContextOfType(expr, XmlTag.class, true);
assert tag != null;
otherMatches.add(tag);
}
}
final Settings dlg = getSettings(expression, matchingExpressions);
if (dlg == null || dlg.isCanceled())
return;
if (getCommandName() != null) {
new WriteCommandAction.Simple(e.getProject(), getCommandName()) {
protected void run() throws Throwable {
if (extractImpl(expression, matchingExpressions, otherMatches, dlg)) {
for (RangeHighlighter highlighter : highlighters) {
highlighter.dispose();
}
}
}
}.execute();
} else {
extractImpl(expression, matchingExpressions, otherMatches, dlg);
}
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class BaseIntroduceAction method actionPerformedImpl.
protected boolean actionPerformedImpl(PsiFile file, Editor editor, XmlAttribute context, int offset) {
if (!(file instanceof XPathFile))
return false;
// pattern attribute may not reference variables
if (XsltSupport.isPatternAttribute(context))
return false;
final SelectionModel selectionModel = editor.getSelectionModel();
final boolean hasSelection = selectionModel.hasSelection();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
if (hasSelection) {
final PsiElement xpathElement = file.findElementAt(start);
if (xpathElement != null) {
XPathExpression expression = PsiTreeUtil.getParentOfType(xpathElement, XPathExpression.class);
while (expression != null) {
if (expression.getTextRange().getStartOffset() == start) {
final int diff = expression.getTextRange().getEndOffset() - end;
if (diff == 0) {
extractFromExpression(editor, expression);
return true;
} else if (diff > 0) {
break;
}
}
expression = PsiTreeUtil.getParentOfType(expression, XPathExpression.class);
}
}
} else {
final XPathExpression expression = PsiTreeUtil.getChildOfType(file, XPathExpression.class);
if (expression != null) {
final PsiFile containingFile = expression.getContainingFile();
assert containingFile != null;
final TextRange range = expression.getTextRange();
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
extractFromExpression(editor, expression);
return true;
}
}
return false;
}
Aggregations