From 1c8b7061ee0fb8a9c416921511a480350b947022 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Wed, 6 Jul 2016 23:12:07 +0300 Subject: [PATCH] Enable custom surrounders for languges without line comments --- .../folding/CustomFoldingSurroundDescriptor.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/lang/folding/CustomFoldingSurroundDescriptor.java b/platform/lang-impl/src/com/intellij/lang/folding/CustomFoldingSurroundDescriptor.java index 684804d521703..cd235cb0b0740 100644 --- a/platform/lang-impl/src/com/intellij/lang/folding/CustomFoldingSurroundDescriptor.java +++ b/platform/lang-impl/src/com/intellij/lang/folding/CustomFoldingSurroundDescriptor.java @@ -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. @@ -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; @@ -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) { @@ -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); @@ -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);