Search in sources :

Example 6 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class CustomFoldingSurroundDescriptor method getElementsToSurround.

@NotNull
@Override
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    if (startOffset >= endOffset)
        return PsiElement.EMPTY_ARRAY;
    Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage());
    if (commenter == null || commenter.getLineCommentPrefix() == null && (commenter.getBlockCommentPrefix() == null || commenter.getBlockCommentSuffix() == null)) {
        return PsiElement.EMPTY_ARRAY;
    }
    PsiElement startElement = file.findElementAt(startOffset);
    PsiElement endElement = file.findElementAt(endOffset - 1);
    if (startElement instanceof PsiWhiteSpace) {
        if (startElement == endElement)
            return PsiElement.EMPTY_ARRAY;
        startElement = startElement.getNextSibling();
    }
    if (endElement instanceof PsiWhiteSpace)
        endElement = endElement.getPrevSibling();
    if (startElement != null && endElement != null) {
        startElement = findClosestParentAfterLineBreak(startElement);
        if (startElement != null) {
            endElement = findClosestParentBeforeLineBreak(endElement);
            if (endElement != null) {
                return adjustRange(startElement, endElement);
            }
        }
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : Commenter(com.intellij.lang.Commenter) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class SuppressionUtil method getBlockPrefixSuffixPair.

@Nullable
public static Couple<String> getBlockPrefixSuffixPair(@NotNull PsiElement comment) {
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage());
    if (commenter != null) {
        final String prefix = commenter.getBlockCommentPrefix();
        final String suffix = commenter.getBlockCommentSuffix();
        if (prefix != null || suffix != null) {
            return Couple.of(StringUtil.notNullize(prefix), StringUtil.notNullize(suffix));
        }
    }
    return null;
}
Also used : Commenter(com.intellij.lang.Commenter) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class PsiParserFacadeImpl method createLineOrBlockCommentFromText.

@Override
@NotNull
public PsiComment createLineOrBlockCommentFromText(@NotNull Language lang, @NotNull String text) throws IncorrectOperationException {
    Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(lang);
    assert commenter != null : lang;
    String prefix = commenter.getLineCommentPrefix();
    final String blockCommentPrefix = commenter.getBlockCommentPrefix();
    final String blockCommentSuffix = commenter.getBlockCommentSuffix();
    assert prefix != null || (blockCommentPrefix != null && blockCommentSuffix != null);
    PsiFile aFile = PsiFileFactory.getInstance(myManager.getProject()).createFileFromText("_Dummy_", lang, prefix != null ? (prefix + text) : (blockCommentPrefix + text + blockCommentSuffix));
    return findPsiCommentChild(aFile);
}
Also used : Commenter(com.intellij.lang.Commenter) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class FileTypeUtil method buildComment.

public static String buildComment(FileType type, String template, LanguageOptions options) {
    Commenter commenter = getCommenter(type);
    if (commenter == null) {
        return "<No comments>";
    }
    String bs = commenter.getBlockCommentPrefix();
    String be = commenter.getBlockCommentSuffix();
    String ls = commenter.getLineCommentPrefix();
    if ((bs == null || be == null) && ls == null) {
        return "<No comments>";
    }
    boolean allowBlock = bs != null && be != null;
    boolean allowLine = ls != null;
    if (allowLine && !allowBlock) {
        bs = ls;
        be = ls;
    }
    boolean allowSeparator = getInstance().allowSeparators(type);
    String filler = options.getFiller();
    if (!allowSeparator) {
        if (options.getFiller() == LanguageOptions.DEFAULT_FILLER) {
            filler = "~";
        }
    }
    boolean isBlock = options.isBlock();
    boolean isPrefix = options.isPrefixLines();
    if (isBlock && !allowBlock) {
        isPrefix = true;
    }
    boolean isBox = options.isBox() && options.isSeparateBefore() && options.isSeparateAfter() && options.getLenBefore() == options.getLenAfter();
    StringBuilder preview = new StringBuilder(80);
    String open = isBlock ? bs : allowLine ? ls : bs;
    String close = isBlock ? be : allowLine ? ls : be;
    StringBuilder pre = new StringBuilder(5);
    StringBuilder leader = new StringBuilder(5);
    StringBuilder post = new StringBuilder(5);
    if (filler == LanguageOptions.DEFAULT_FILLER) {
        filler = open.substring(open.length() - 1);
    }
    int offset = 0;
    if (isBlock) {
        int pos = open.length() - 1;
        pre.append(allowBlock ? filler : open.charAt(pos));
        while (pos > 0 && open.charAt(pos) == open.charAt(open.length() - 1)) {
            pos--;
            offset++;
        }
        while (open.length() > 1 && pos >= 0) {
            leader.append(' ');
            pos--;
        }
        post.append(filler);
        if (!isPrefix) {
            pre = new StringBuilder(0);
        }
        if (!allowBlock) {
            close = filler;
        }
    } else {
        if (allowLine) {
            close = filler;
        }
        pre.append(open);
        post.append(close);
    }
    int diff = 0;
    if (options.isSeparateBefore()) {
        if (isBlock && isBox && allowBlock) {
            diff = close.length() - offset;
        }
        preview.append(open);
        for (int i = open.length() + 1; i <= options.getLenBefore() - diff - post.length(); i++) {
            preview.append(filler);
        }
        preview.append(post);
        preview.append('\n');
    } else if (isBlock) {
        preview.append(open).append('\n');
    }
    if (!template.isEmpty()) {
        String[] lines = template.split("\n", -1);
        for (String line : lines) {
            if (options.isTrim()) {
                line = line.trim();
            }
            line = StringUtil.trimStart(StringUtil.trimStart(line, pre.toString()), open);
            line = StringUtil.trimEnd(line, close);
            preview.append(leader).append(pre);
            int len = 0;
            if (pre.length() > 0 && !line.isEmpty()) {
                preview.append(' ');
                len++;
            }
            preview.append(line);
            len += line.length() + leader.length() + pre.length();
            if (isBox && len < options.getLenBefore() - diff) {
                for (; len < options.getLenBefore() - diff - post.length(); len++) {
                    preview.append(' ');
                }
                if (isBlock || allowLine) {
                    preview.append(post.substring(0, options.getLenBefore() - diff - len));
                }
            }
            if (!isBlock && !allowLine) {
                if (preview.charAt(preview.length() - 1) != ' ') {
                    preview.append(' ');
                }
                preview.append(close);
            }
            preview.append('\n');
        }
    }
    preview.append(leader);
    if (options.isSeparateAfter()) {
        preview.append(pre);
        for (int i = leader.length() + pre.length(); i < options.getLenAfter() - close.length(); i++) {
            preview.append(filler);
        }
        preview.append(close);
        preview.append('\n');
    } else if (isBlock) {
        if (!allowBlock) {
            preview.append(pre).append('\n');
        } else {
            preview.append(close).append('\n');
        }
    }
    return preview.substring(0, preview.length() - 1);
}
Also used : Commenter(com.intellij.lang.Commenter)

Example 10 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class FileTypeUtil method isSupportedType.

private static boolean isSupportedType(FileType type) {
    if (type.isBinary() || type.getName().contains("IDEA") || "GUI_DESIGNER_FORM".equals(type.getName())) {
        return false;
    } else {
        Commenter commenter = getCommenter(type);
        boolean hasComment = commenter != null && (commenter.getLineCommentPrefix() != null || commenter.getBlockCommentPrefix() != null);
        if (!hasComment) {
            return false;
        }
        if (type.equals(StdFileTypes.DTD)) {
            return true;
        }
        if (type.equals(StdFileTypes.HTML)) {
            return true;
        }
        if (type.equals(StdFileTypes.XHTML)) {
            return true;
        }
        if (type.equals(StdFileTypes.PROPERTIES)) {
            return true;
        }
        return CopyrightUpdaters.INSTANCE.forFileType(type) != null;
    }
}
Also used : Commenter(com.intellij.lang.Commenter)

Aggregations

Commenter (com.intellij.lang.Commenter)22 NotNull (org.jetbrains.annotations.NotNull)6 Language (com.intellij.lang.Language)4 PsiFile (com.intellij.psi.PsiFile)4 Nullable (org.jetbrains.annotations.Nullable)3 CodeDocumentationAwareCommenter (com.intellij.lang.CodeDocumentationAwareCommenter)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiComment (com.intellij.psi.PsiComment)2 PsiElement (com.intellij.psi.PsiElement)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 SyntaxTable (com.intellij.ide.highlighter.custom.SyntaxTable)1 InjectedCaret (com.intellij.injected.editor.InjectedCaret)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 FileType (com.intellij.openapi.fileTypes.FileType)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)1 IElementType (com.intellij.psi.tree.IElementType)1 XmlTag (com.intellij.psi.xml.XmlTag)1