ShowExpressionTypeAction: extension point & providers

This commit is contained in:
Gregory.Shrago
2015-06-09 23:03:17 +03:00
parent f0d2784af4
commit a07577abce
10 changed files with 294 additions and 0 deletions
@@ -0,0 +1,51 @@
/*
* 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.hint;
import com.intellij.lang.ExpressionTypeProvider;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiType;
import com.intellij.psi.SyntaxTraverser;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* @author gregsh
*/
public class JavaTypeProvider extends ExpressionTypeProvider<PsiExpression> {
@NotNull
@Override
public String getInformationHint(@NotNull PsiExpression element) {
PsiType type = element.getType();
String text = type == null ? "<unknown>" : type.getCanonicalText();
return StringUtil.escapeXml(text);
}
@NotNull
@Override
public String getErrorHint() {
return "No expression found";
}
@NotNull
@Override
public List<PsiExpression> getExpressionsAt(@NotNull PsiElement elementAt) {
return SyntaxTraverser.psiTraverser().parents(elementAt).filter(PsiExpression.class).toList();
}
}
@@ -0,0 +1,35 @@
/*
* 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.lang;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* @author gregsh
*/
public abstract class ExpressionTypeProvider<T extends PsiElement> {
@NotNull
public abstract String getInformationHint(@NotNull T element);
@NotNull
public abstract String getErrorHint();
@NotNull
public abstract List<T> getExpressionsAt(@NotNull PsiElement elementAt);
}
@@ -0,0 +1,28 @@
/*
* 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.
*/
/*
* @author max
*/
package com.intellij.lang;
public class LanguageExpressionTypes extends LanguageExtension<ExpressionTypeProvider> {
public static final LanguageExpressionTypes INSTANCE = new LanguageExpressionTypes();
private LanguageExpressionTypes() {
super("com.intellij.codeInsight.typeInfo");
}
}
@@ -0,0 +1,123 @@
/*
* 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.hint;
import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.lang.ExpressionTypeProvider;
import com.intellij.lang.Language;
import com.intellij.lang.LanguageExpressionTypes;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.refactoring.IntroduceTargetChooser;
import com.intellij.util.Function;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.JBIterable;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
import java.util.Set;
public class ShowExpressionTypeHandler implements CodeInsightActionHandler {
@Override
public boolean startInWriteAction() {
return false;
}
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
ApplicationManager.getApplication().assertIsDispatchThread();
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiElement elementAt = file.findElementAt(
TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset()));
if (elementAt == null) return;
Language language = elementAt.getLanguage();
final Set<ExpressionTypeProvider> handlers = getHandlers(project, language, file.getViewProvider().getBaseLanguage());
if (handlers.isEmpty()) return;
TextRange range = EditorUtil.getSelectionInAnyMode(editor);
final Map<PsiElement, ExpressionTypeProvider> map = ContainerUtil.newLinkedHashMap();
for (ExpressionTypeProvider handler : handlers) {
for (PsiElement element : ((ExpressionTypeProvider<? extends PsiElement>)handler).getExpressionsAt(elementAt)) {
if (!element.getTextRange().contains(range)) continue;
map.put(element, handler);
}
}
Pass<PsiElement> callback = new Pass<PsiElement>() {
@Override
public void pass(@NotNull PsiElement expression) {
//noinspection unchecked
ExpressionTypeProvider<PsiElement> provider = ObjectUtils.assertNotNull(map.get(expression));
final String informationHint = provider.getInformationHint(expression);
TextRange range = expression.getTextRange();
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
HintManager.getInstance().showInformationHint(editor, informationHint);
}
});
}
};
if (map.isEmpty()) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
String errorHint = ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(handlers)).getErrorHint();
HintManager.getInstance().showErrorHint(editor, errorHint);
}
});
}
else if (map.size() == 1) {
callback.pass(ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(map.keySet())));
}
else {
IntroduceTargetChooser.showChooser(
editor, ContainerUtil.newArrayList(map.keySet()), callback,
new Function<PsiElement, String>() {
@Override
public String fun(@NotNull PsiElement expression) {
return expression.getText();
}
}
);
}
}
@NotNull
public static Set<ExpressionTypeProvider> getHandlers(final Project project, Language... languages) {
return JBIterable.of(languages).flatten(new Function<Language, Iterable<ExpressionTypeProvider>>() {
@Override
public Iterable<ExpressionTypeProvider> fun(Language language) {
return DumbService.getInstance(project).filterByDumbAwareness(LanguageExpressionTypes.INSTANCE.allForLanguage(language));
}
}).addAllTo(ContainerUtil.<ExpressionTypeProvider>newLinkedHashSet());
}
}
@@ -0,0 +1,47 @@
/*
* 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.hint.actions;
import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.codeInsight.actions.BaseCodeInsightAction;
import com.intellij.codeInsight.hint.ShowExpressionTypeHandler;
import com.intellij.lang.Language;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilCore;
import org.jetbrains.annotations.NotNull;
public class ShowExpressionTypeAction extends BaseCodeInsightAction implements DumbAware {
public ShowExpressionTypeAction() {
setEnabledInModalContext(true);
}
@NotNull
@Override
protected CodeInsightActionHandler getHandler() {
return new ShowExpressionTypeHandler();
}
@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull final PsiFile file) {
Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
return !ShowExpressionTypeHandler.getHandlers(project, language, file.getViewProvider().getBaseLanguage()).isEmpty();
}
}
@@ -392,6 +392,8 @@ action.ShowSiblings.text=Show Siblings
action.ShowSiblings.description=Show a popup window with the symbol siblings content
action.ParameterInfo.text=_Parameter Info
action.ParameterInfo.description=Show parameters of the method call at caret
action.TypeInfo.text=_Type Info
action.TypeInfo.description=Show type of the selected expression
action.EditorContextInfo.text=_Context Info
action.EditorContextInfo.description=Show the current method or class declaration when it is not visible
action.ShowErrorDescription.text=E_rror Description
@@ -556,6 +556,9 @@
<extensionPoint name="codeInsight.parameterInfo" beanClass="com.intellij.lang.LanguageExtensionPoint">
<with attribute="implementationClass" implements="com.intellij.lang.parameterInfo.ParameterInfoHandler"/>
</extensionPoint>
<extensionPoint name="codeInsight.typeInfo" beanClass="com.intellij.lang.LanguageExtensionPoint">
<with attribute="implementationClass" implements="com.intellij.lang.ExpressionTypeProvider"/>
</extensionPoint>
<extensionPoint name="codeInsight.fillParagraph" beanClass="com.intellij.lang.LanguageExtensionPoint">
<with attribute="implementationClass" implements="com.intellij.codeInsight.editorActions.fillParagraph.ParagraphFillHandler"/>
@@ -422,6 +422,9 @@
<action id="ParameterInfo">
<keyboard-shortcut first-keystroke="control P"/>
</action>
<action id="TypeInfo">
<keyboard-shortcut first-keystroke="control shift P"/>
</action>
<action id="ChangeSignature">
<keyboard-shortcut first-keystroke="control F6"/>
</action>
@@ -165,6 +165,7 @@
<group id="CodeEditorViewGroup">
<action id="ExternalJavaDoc" class="com.intellij.ide.actions.ExternalJavaDocAction"/>
<action id="ParameterInfo" class="com.intellij.codeInsight.hint.actions.ShowParameterInfoAction"/>
<action id="TypeInfo" class="com.intellij.codeInsight.hint.actions.ShowExpressionTypeAction"/>
<action id="EditorContextInfo" class="com.intellij.codeInsight.hint.actions.ShowContainerInfoAction"/>
<action id="ShowErrorDescription" class="com.intellij.codeInsight.daemon.impl.actions.ShowErrorDescriptionAction"/>
</group>
+1
View File
@@ -1028,6 +1028,7 @@
<codeInsight.parameterInfo language="JAVA" implementationClass="com.intellij.codeInsight.hint.api.impls.ReferenceParameterInfoHandler"/>
<codeInsight.parameterInfo language="JAVA"
implementationClass="com.intellij.codeInsight.hint.api.impls.AnnotationParameterInfoHandler"/>
<codeInsight.typeInfo language="JAVA" implementationClass="com.intellij.codeInsight.hint.JavaTypeProvider"/>
<codeInsight.overrideMethod language="JAVA" implementationClass="com.intellij.codeInsight.generation.JavaOverrideMethodsHandler"/>
<codeInsight.implementMethod language="JAVA" implementationClass="com.intellij.codeInsight.generation.JavaImplementMethodsHandler"/>