Search in sources :

Example 31 with IntArrayList

use of com.intellij.util.containers.IntArrayList in project intellij-community by JetBrains.

the class CommentByLineCommentHandler method doCommentLine.

private static boolean doCommentLine(Block block, int line, int offset, int endOffset, Commenter commenter, Document document) {
    String prefix = commenter.getLineCommentPrefix();
    int shiftedStartOffset = CharArrayUtil.shiftForward(document.getCharsSequence(), offset, " \t");
    if (prefix != null) {
        if (commenter instanceof CommenterWithLineSuffix) {
            endOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), endOffset, " \t");
            String lineSuffix = ((CommenterWithLineSuffix) commenter).getLineCommentSuffix();
            if (!CharArrayUtil.regionMatches(document.getCharsSequence(), shiftedStartOffset, prefix)) {
                if (!CharArrayUtil.regionMatches(document.getCharsSequence(), endOffset - lineSuffix.length(), lineSuffix)) {
                    document.insertString(endOffset, lineSuffix);
                }
                document.insertString(offset, prefix);
            }
        } else {
            if (block.addSpace && shiftedStartOffset < document.getTextLength() && document.getCharsSequence().charAt(shiftedStartOffset) != '\n') {
                prefix += ' ';
            }
            document.insertString(offset, prefix);
        }
    } else {
        prefix = commenter.getBlockCommentPrefix();
        String suffix = commenter.getBlockCommentSuffix();
        if (prefix == null || suffix == null)
            return true;
        if (endOffset == offset && block.startLine != block.endLine)
            return true;
        final int textLength = document.getTextLength();
        final CharSequence chars = document.getCharsSequence();
        offset = CharArrayUtil.shiftForward(chars, offset, " \t");
        if (endOffset == textLength) {
            final int shifted = CharArrayUtil.shiftBackward(chars, textLength - 1, " \t") + 1;
            if (shifted < textLength)
                endOffset = shifted;
        } else {
            endOffset = CharArrayUtil.shiftBackward(chars, endOffset, " \t");
        }
        if (endOffset < offset || offset == textLength - 1 && line != document.getLineCount() - 1) {
            return true;
        }
        final String text = chars.subSequence(offset, endOffset).toString();
        final IntArrayList prefixes = new IntArrayList();
        final IntArrayList suffixes = new IntArrayList();
        final String commentedSuffix = commenter.getCommentedBlockCommentSuffix();
        final String commentedPrefix = commenter.getCommentedBlockCommentPrefix();
        for (int position = 0; position < text.length(); ) {
            int nearestPrefix = text.indexOf(prefix, position);
            if (nearestPrefix == -1) {
                nearestPrefix = text.length();
            }
            int nearestSuffix = text.indexOf(suffix, position);
            if (nearestSuffix == -1) {
                nearestSuffix = text.length();
            }
            if (Math.min(nearestPrefix, nearestSuffix) == text.length()) {
                break;
            }
            if (nearestPrefix < nearestSuffix) {
                prefixes.add(nearestPrefix);
                position = nearestPrefix + prefix.length();
            } else {
                suffixes.add(nearestSuffix);
                position = nearestSuffix + suffix.length();
            }
        }
        if (!(commentedSuffix == null && !suffixes.isEmpty() && offset + suffixes.get(suffixes.size() - 1) + suffix.length() >= endOffset)) {
            document.insertString(endOffset, suffix);
        }
        int nearestPrefix = prefixes.size() - 1;
        int nearestSuffix = suffixes.size() - 1;
        while (nearestPrefix >= 0 || nearestSuffix >= 0) {
            if (nearestSuffix == -1 || nearestPrefix != -1 && prefixes.get(nearestPrefix) > suffixes.get(nearestSuffix)) {
                final int position = prefixes.get(nearestPrefix);
                nearestPrefix--;
                if (commentedPrefix != null) {
                    document.replaceString(offset + position, offset + position + prefix.length(), commentedPrefix);
                } else if (position != 0) {
                    document.insertString(offset + position, suffix);
                }
            } else {
                final int position = suffixes.get(nearestSuffix);
                nearestSuffix--;
                if (commentedSuffix != null) {
                    document.replaceString(offset + position, offset + position + suffix.length(), commentedSuffix);
                } else if (offset + position + suffix.length() < endOffset) {
                    document.insertString(offset + position + suffix.length(), prefix);
                }
            }
        }
        if (!(commentedPrefix == null && !prefixes.isEmpty() && prefixes.get(0) == 0)) {
            document.insertString(offset, prefix);
        }
    }
    return false;
}
Also used : IntArrayList(com.intellij.util.containers.IntArrayList)

Aggregations

IntArrayList (com.intellij.util.containers.IntArrayList)31 ArrayList (java.util.ArrayList)5 PsiFile (com.intellij.psi.PsiFile)3 NotNull (org.jetbrains.annotations.NotNull)3 TextRange (com.intellij.openapi.util.TextRange)2 Nullable (org.jetbrains.annotations.Nullable)2 ExceptionUtil (com.intellij.codeInsight.ExceptionUtil)1 RadComponent (com.intellij.designer.model.RadComponent)1 LighterASTNode (com.intellij.lang.LighterASTNode)1 Logger (com.intellij.openapi.diagnostic.Logger)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 Project (com.intellij.openapi.project.Project)1 com.intellij.psi (com.intellij.psi)1 PsiCodeBlock (com.intellij.psi.PsiCodeBlock)1 PsiElement (com.intellij.psi.PsiElement)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1 DummyHolder (com.intellij.psi.impl.source.DummyHolder)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 PsiUtil (com.intellij.psi.util.PsiUtil)1