Groovy: Quick info for methods. Use links for parameter types

This commit is contained in:
Max Medvedev
2012-12-05 11:54:11 +04:00
parent 2cc0e2fc76
commit 049f3bd8fd
5 changed files with 50 additions and 18 deletions
@@ -153,7 +153,7 @@ public class GroovyDocumentationProvider implements CodeDocumentationProvider, E
PsiParameter parameter = parameters[i];
if (i > 0) buffer.append(", ");
if (parameter instanceof GrParameter) {
GroovyPresentationUtil.appendParameterPresentation((GrParameter)parameter, substitutor, false, buffer);
GroovyPresentationUtil.appendParameterPresentation((GrParameter)parameter, substitutor, TypePresentation.LINK, buffer);
}
else {
PsiType type = parameter.getType();
@@ -311,7 +311,7 @@ public class GroovyDocumentationProvider implements CodeDocumentationProvider, E
}
private static void appendTypeString(StringBuilder buffer, final PsiType type, PsiElement context) {
public static void appendTypeString(StringBuilder buffer, final PsiType type, PsiElement context) {
if (type != null) {
JavaDocInfoGenerator.generateType(buffer, type, context);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -35,24 +35,30 @@ import java.util.Set;
public class GroovyPresentationUtil {
private static final int CONSTRAINTS_NUMBER = 2;
public static String getParameterPresentation(GrParameter parameter, PsiSubstitutor substitutor, boolean presentable) {
StringBuilder builder = new StringBuilder();
appendParameterPresentation(parameter, substitutor, presentable, builder);
return builder.toString();
}
public static void appendParameterPresentation(GrParameter parameter,
PsiSubstitutor substitutor,
boolean presentable,
TypePresentation typePresentation,
StringBuilder builder) {
for (PsiAnnotation annotation : parameter.getModifierList().getAnnotations()) {
builder.append(annotation.getText()).append(' ');
}
PsiType type = parameter.getTypeGroovy();
type = substitutor.substitute(type);
if (typePresentation == TypePresentation.LINK) {
GroovyDocumentationProvider.appendTypeString(builder, type, parameter);
builder.append(' ').append(parameter.getName());
return;
}
if (type != null) {
type = substitutor.substitute(type);
builder.append(presentable ? type.getPresentableText() : type.getCanonicalText()).append(' ').append(parameter.getName());
if (typePresentation == TypePresentation.PRESENTABLE) {
builder.append(type.getPresentableText()).append(' ').append(parameter.getName());
}
else if (typePresentation == TypePresentation.CANONICAL) {
builder.append(type.getCanonicalText()).append(' ').append(parameter.getName());
}
}
else {
builder.append(parameter.getName());
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2012 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 org.jetbrains.plugins.groovy.lang.documentation;
/**
* @author Max Medvedev
*/
public enum TypePresentation {
PRESENTABLE, CANONICAL, LINK
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -29,6 +29,7 @@ import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.documentation.GroovyPresentationUtil;
import org.jetbrains.plugins.groovy.lang.documentation.TypePresentation;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
@@ -447,7 +448,7 @@ public class GroovyParameterInfoHandler implements ParameterInfoHandlerWithTabAc
private static void appendParameterText(PsiParameter param, PsiSubstitutor substitutor, StringBuilder buffer) {
if (param instanceof GrParameter) {
GrParameter grParam = (GrParameter)param;
GroovyPresentationUtil.appendParameterPresentation(grParam, substitutor, true, buffer);
GroovyPresentationUtil.appendParameterPresentation(grParam, substitutor, TypePresentation.PRESENTABLE, buffer);
final GrExpression initializer = grParam.getInitializerGroovy();
if (initializer != null) {
@@ -29,8 +29,9 @@ class Bar<T> { java.util.List<T> foo(T param); }
new Bar<String>().f<caret>oo();
'''
def ref = myFixture.file.findReferenceAt(myFixture.editor.caretModel.offset)
assert CtrlMouseHandler.getInfo(ref.resolve(), ref.element) == """Bar
java.util.List&lt;java.lang.String&gt; foo (java.lang.String param)"""
assert CtrlMouseHandler.getInfo(ref.resolve(), ref.element) == """\
Bar
<a href="psi_element://java.util.List"><code>List</code></a>&lt;<a href="psi_element://java.lang.String"><code>String</code></a>&gt; foo (<a href="psi_element://java.lang.String"><code>String</code></a> param)"""
}
public void testGenericField() {
@@ -39,8 +40,9 @@ class Bar<T> { T field; }
new Bar<Integer>().fi<caret>eld
'''
def ref = myFixture.file.findReferenceAt(myFixture.editor.caretModel.offset)
assert CtrlMouseHandler.getInfo(ref.resolve(), ref.element) == """Bar
java.lang.Integer getField ()"""
assert CtrlMouseHandler.getInfo(ref.resolve(), ref.element) == """\
Bar
<a href="psi_element://java.lang.Integer"><code>Integer</code></a> getField ()"""
}
}