From 98e7a391f0ca9cb45613b2008e8fd9f2f1971c9c Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Wed, 23 Nov 2016 13:03:35 +0300 Subject: [PATCH] IDEA-163970 Extend selection in block comments --- .../BlockCommentSelectioner.java | 49 +++++++++++++++++++ .../src/META-INF/LangExtensions.xml | 1 + 2 files changed, 50 insertions(+) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/BlockCommentSelectioner.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/BlockCommentSelectioner.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/BlockCommentSelectioner.java new file mode 100644 index 000000000000..873335b29e03 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/BlockCommentSelectioner.java @@ -0,0 +1,49 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.editorActions.wordSelection; + +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandler; +import com.intellij.lang.Commenter; +import com.intellij.lang.LanguageCommenters; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiComment; +import com.intellij.psi.PsiDocCommentBase; +import com.intellij.psi.PsiElement; + +import java.util.Collections; +import java.util.List; + +public class BlockCommentSelectioner implements ExtendWordSelectionHandler { + @Override + public boolean canSelect(PsiElement e) { + return e instanceof PsiComment && !(e instanceof PsiDocCommentBase); + } + + @Override + public List select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { + Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(e.getLanguage()); + if (commenter == null) return null; + String blockStart = commenter.getBlockCommentPrefix(); + String blockEnd = commenter.getBlockCommentSuffix(); + if (blockStart == null || blockEnd == null) return null; + String elementText = e.getText(); + if (elementText == null || !elementText.startsWith(blockStart) || !elementText.endsWith(blockEnd)) return null; + TextRange elementRange = e.getTextRange(); + return Collections.singletonList(new TextRange(elementRange.getStartOffset() + blockStart.length(), + elementRange.getEndOffset() - blockEnd.length())); + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 14ac99c52491..2d25372ab61a 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -620,6 +620,7 @@ +