Search in sources :

Example 61 with TextRange

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));
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 62 with TextRange

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextRange(com.intellij.openapi.util.TextRange) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with TextRange

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));
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 64 with TextRange

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);
    }
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 65 with TextRange

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;
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) XPathFile(org.intellij.lang.xpath.XPathFile) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1314 PsiElement (com.intellij.psi.PsiElement)261 NotNull (org.jetbrains.annotations.NotNull)237 Nullable (org.jetbrains.annotations.Nullable)167 Document (com.intellij.openapi.editor.Document)132 ArrayList (java.util.ArrayList)117 Project (com.intellij.openapi.project.Project)96 PsiFile (com.intellij.psi.PsiFile)96 ASTNode (com.intellij.lang.ASTNode)95 Editor (com.intellij.openapi.editor.Editor)75 PsiReference (com.intellij.psi.PsiReference)54 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 IElementType (com.intellij.psi.tree.IElementType)48 IncorrectOperationException (com.intellij.util.IncorrectOperationException)48 Pair (com.intellij.openapi.util.Pair)47 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)33 ProperTextRange (com.intellij.openapi.util.ProperTextRange)32 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)31 XmlTag (com.intellij.psi.xml.XmlTag)31 List (java.util.List)31