From c2901947a3885636bbf3cd75df80c24f82140456 Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Wed, 16 Sep 2015 22:02:23 +0300 Subject: [PATCH] introduce "word selectioner" for path parts in a string literal --- .../InjectedReferenceSelectioner.java | 54 +++++++++++++++++++ .../src/META-INF/LangExtensions.xml | 1 + 2 files changed, 55 insertions(+) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/InjectedReferenceSelectioner.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/InjectedReferenceSelectioner.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/InjectedReferenceSelectioner.java new file mode 100644 index 000000000000..04eb02fdfdba --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/wordSelection/InjectedReferenceSelectioner.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2015 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.openapi.editor.Editor; +import com.intellij.openapi.util.Conditions; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiLanguageInjectionHost; +import com.intellij.psi.PsiReference; +import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.Function; +import com.intellij.util.containers.JBIterable; + +import java.util.Collections; +import java.util.List; + +public class InjectedReferenceSelectioner extends AbstractWordSelectioner { + @Override + public boolean canSelect(PsiElement e) { + return PsiTreeUtil.getParentOfType(e, PsiLanguageInjectionHost.class) != null; + } + + @Override + public List select(PsiElement e, CharSequence editorText, final int cursorOffset, Editor editor) { + PsiReference ref = e.findReferenceAt(cursorOffset - e.getTextRange().getStartOffset()); + if (ref == null) return Collections.emptyList(); + JBIterable it = ref instanceof PsiMultiReference ? JBIterable.of(((PsiMultiReference)ref).getReferences()) : JBIterable.of(ref); + return it.transform(new Function() { + @Override + public TextRange fun(PsiReference ref) { + TextRange base = ref.getElement().getTextRange(); + TextRange r = ref.getRangeInElement().shiftRight(base.getStartOffset()); + return r.containsOffset(cursorOffset) ? r : null; + } + }).filter(Conditions.notNull()).toList(); + } + +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 26f129b6c71e..c4fd589d31c0 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -587,6 +587,7 @@ +