Search in sources :

Example 11 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class YAMLQuotedTextImpl method getContentRanges.

@NotNull
@Override
public List<TextRange> getContentRanges() {
    final ASTNode firstContentNode = getFirstContentNode();
    if (firstContentNode == null) {
        return Collections.emptyList();
    }
    List<TextRange> result = new ArrayList<>();
    TextRange contentRange = TextRange.create(firstContentNode.getStartOffset(), getTextRange().getEndOffset()).shiftRight(-getTextRange().getStartOffset());
    final List<String> lines = StringUtil.split(contentRange.substring(getText()), "\n", true, false);
    // First line has opening quote
    int cumulativeOffset = contentRange.getStartOffset();
    for (int i = 0; i < lines.size(); ++i) {
        final String line = lines.get(i);
        int lineStart = 0;
        int lineEnd = line.length();
        if (i == 0) {
            lineStart++;
        } else {
            while (lineStart < line.length() && YAMLGrammarCharUtil.isSpaceLike(line.charAt(lineStart))) {
                lineStart++;
            }
        }
        if (i == lines.size() - 1) {
            // Last line has closing quote
            lineEnd--;
        } else {
            while (lineEnd > lineStart && YAMLGrammarCharUtil.isSpaceLike(line.charAt(lineEnd - 1))) {
                lineEnd--;
            }
        }
        result.add(TextRange.create(lineStart, lineEnd).shiftRight(cumulativeOffset));
        cumulativeOffset += line.length() + 1;
    }
    return result;
}
Also used : ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class PyConsoleUtil method createTabCompletionAction.

public static AnAction createTabCompletionAction(PythonConsoleView consoleView) {
    final AnAction runCompletions = new AnAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            Editor editor = consoleView.getConsoleEditor();
            if (LookupManager.getActiveLookup(editor) != null) {
                AnAction replace = ActionManager.getInstance().getAction(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_REPLACE);
                ActionUtil.performActionDumbAware(replace, e);
                return;
            }
            AnAction completionAction = ActionManager.getInstance().getAction(IdeActions.ACTION_CODE_COMPLETION);
            if (completionAction != null) {
                ActionUtil.performActionDumbAware(completionAction, e);
            }
        }

        @Override
        public void update(AnActionEvent e) {
            Editor editor = consoleView.getConsoleEditor();
            if (LookupManager.getActiveLookup(editor) != null) {
                e.getPresentation().setEnabled(false);
            }
            int offset = editor.getCaretModel().getOffset();
            Document document = editor.getDocument();
            int lineStart = document.getLineStartOffset(document.getLineNumber(offset));
            String textToCursor = document.getText(new TextRange(lineStart, offset));
            e.getPresentation().setEnabled(!CharMatcher.WHITESPACE.matchesAllOf(textToCursor));
        }
    };
    runCompletions.registerCustomShortcutSet(KeyEvent.VK_TAB, 0, consoleView.getConsoleEditor().getComponent());
    runCompletions.getTemplatePresentation().setVisible(false);
    return runCompletions;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) AnAction(com.intellij.openapi.actionSystem.AnAction)

Example 13 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class PyTestCase method doPerformFormatting.

private void doPerformFormatting() throws IncorrectOperationException {
    final PsiFile file = myFixture.getFile();
    final TextRange myTextRange = file.getTextRange();
    CodeStyleManager.getInstance(myFixture.getProject()).reformatText(file, myTextRange.getStartOffset(), myTextRange.getEndOffset());
}
Also used : TextRange(com.intellij.openapi.util.TextRange)

Example 14 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class SpellCheckerDictionaryGenerator method processLeafsNames.

protected void processLeafsNames(@NotNull final PsiElement leafElement, @NotNull final HashSet<String> seenNames) {
    final Language language = leafElement.getLanguage();
    SpellCheckingInspection.tokenize(leafElement, language, new TokenConsumer() {

        @Override
        public void consumeToken(PsiElement element, final String text, boolean useRename, int offset, TextRange rangeToCheck, Splitter splitter) {
            splitter.split(text, rangeToCheck, textRange -> {
                final String word = textRange.substring(text);
                addSeenWord(seenNames, word, language);
            });
        }
    });
}
Also used : TokenConsumer(com.intellij.spellchecker.tokenizer.TokenConsumer) Language(com.intellij.lang.Language) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NamesValidator(com.intellij.lang.refactoring.NamesValidator) SpellCheckingInspection(com.intellij.spellchecker.inspections.SpellCheckingInspection) PsiManager(com.intellij.psi.PsiManager) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) TokenConsumer(com.intellij.spellchecker.tokenizer.TokenConsumer) FileWriter(java.io.FileWriter) IOException(java.io.IOException) TextRange(com.intellij.openapi.util.TextRange) LanguageNamesValidation(com.intellij.lang.LanguageNamesValidation) File(java.io.File) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) Splitter(com.intellij.spellchecker.inspections.Splitter) NotNull(org.jetbrains.annotations.NotNull) SpellCheckerManager(com.intellij.spellchecker.SpellCheckerManager) Consumer(com.intellij.util.Consumer) Splitter(com.intellij.spellchecker.inspections.Splitter) Language(com.intellij.lang.Language) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 15 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class RenameTagBeginOrEndIntentionAction method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null)
        return;
    if (psiElement instanceof PsiWhiteSpace)
        psiElement = PsiTreeUtil.prevLeaf(psiElement);
    if (psiElement instanceof XmlToken) {
        final IElementType tokenType = ((XmlToken) psiElement).getTokenType();
        if (tokenType != XmlTokenType.XML_NAME) {
            if (tokenType == XmlTokenType.XML_TAG_END) {
                psiElement = psiElement.getPrevSibling();
                if (psiElement == null)
                    return;
            }
        }
        PsiElement target = null;
        final String text = psiElement.getText();
        if (!myTargetName.equals(text)) {
            target = psiElement;
        } else {
            // we're in the other
            target = findOtherSide(psiElement, myStart);
        }
        if (target != null) {
            final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
            if (document != null) {
                final TextRange textRange = target.getTextRange();
                document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myTargetName);
            }
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken)

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