Skip to content

Commit

Permalink
Enable custom surrounders for languges without line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zolotov committed Jul 8, 2016
1 parent b88d3c2 commit 1c8b706
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,7 @@
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -65,7 +66,10 @@ public class CustomFoldingSurroundDescriptor implements SurroundDescriptor {
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) return PsiElement.EMPTY_ARRAY;
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) {
Expand Down Expand Up @@ -279,9 +283,11 @@ public TextRange surroundElements(@NotNull Project project, @NotNull Editor edit
Language language = psiFile.getLanguage();
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
if (commenter == null) return null;
String linePrefix = commenter.getLineCommentPrefix();
String linePrefix = ObjectUtils.chooseNotNull(commenter.getLineCommentPrefix(), commenter.getBlockCommentPrefix());
if (linePrefix == null) return null;
int prefixLength = linePrefix.length();
String lineSuffix = StringUtil.notNullize(commenter.getBlockCommentSuffix());

int startOffset = firstElement.getTextRange().getStartOffset();
final Document document = editor.getDocument();
final int startLineNumber = document.getLineNumber(startOffset);
Expand All @@ -296,8 +302,8 @@ public TextRange surroundElements(@NotNull Project project, @NotNull Editor edit
rangeToSelect = TextRange.from(startOffset + descPos, DEFAULT_DESC_TEXT.length());
}

String startString = linePrefix + startText + "\n" + startIndent;
String endString = "\n" + linePrefix + myProvider.getEndString();
String startString = linePrefix + startText + lineSuffix + "\n" + startIndent;
String endString = "\n" + linePrefix + myProvider.getEndString() + lineSuffix;
document.insertString(endOffset, endString);
delta += endString.length();
document.insertString(startOffset, startString);
Expand Down

0 comments on commit 1c8b706

Please sign in to comment.