Skip to content

Commit

Permalink
introduce SpecType, fix #1739
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatov committed Jul 22, 2015
1 parent 9dc912d commit 44a078d
Show file tree
Hide file tree
Showing 34 changed files with 843 additions and 699 deletions.
4 changes: 4 additions & 0 deletions gen/com/goide/GoTypes.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions gen/com/goide/parser/GoParser.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions gen/com/goide/psi/GoSpecType.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions gen/com/goide/psi/GoTypeSpec.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions gen/com/goide/psi/GoVisitor.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions gen/com/goide/psi/impl/GoSpecTypeImpl.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions gen/com/goide/psi/impl/GoTypeSpecImpl.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions grammars/go.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,12 @@ private ExpressionListRecover ::= !('!' | '!=' | '%' | '%=' | '&&' | '&' | '&='

TypeDeclaration ::= 'type' ( TypeSpec | '(' TypeSpecs? ')' ) {pin(".*")=1}
private TypeSpecs ::= TypeSpec (semi TypeSpec)* semi? {pin=1}
TypeSpec ::= identifier Type {
TypeSpec ::= SpecType {
pin=1
methods=[getGoTypeInner getMethods shouldGoDeeper]
methods=[getGoTypeInner getMethods shouldGoDeeper identifier="SpecType/identifier"]
stubClass="com.goide.stubs.GoTypeSpecStub"
}
SpecType ::= identifier Type

VarDeclaration ::= var ( VarSpec | '(' VarSpecs? ')' ) {pin=1}
private VarSpecs ::= VarSpec (semi VarSpec)* semi? {pin=1}
Expand Down
2 changes: 1 addition & 1 deletion src/com/goide/completion/GoCompletionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static LookupElement createTypeConversionLookupElement(@NotNull GoTypeSpe

@NotNull
public static InsertHandler<LookupElement> getTypeConversionInsertHandler(@NotNull GoTypeSpec t) {
GoType type = t.getType();
GoType type = t.getSpecType().getType();
return type instanceof GoStructType || type instanceof GoArrayOrSliceType || type instanceof GoMapType
? BracesInsertHandler.ONE_LINER
: ParenthesesInsertHandler.WITH_PARAMETERS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static ElementPattern<? extends PsiElement> insideSwitchStatement() {

private static ElementPattern<? extends PsiElement> typeDeclaration() {
return psiElement(GoTypes.IDENTIFIER)
.withParent(psiElement(GoTypeReferenceExpression.class).withParent(psiElement(GoType.class).withParent(GoTypeSpec.class)));
.withParent(psiElement(GoTypeReferenceExpression.class).withParent(psiElement(GoType.class).withParent(GoSpecType.class)));
}

private static PsiElementPattern.Capture<PsiElement> insideGoOrDeferStatements(@NotNull IElementType tokenType) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/goide/editor/GoFoldingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull D
}

for (GoTypeSpec type : file.getTypes()) {
foldTypes(type.getType(), result);
foldTypes(type.getSpecType().getType(), result);
}

if (!quick) {
Expand Down
6 changes: 3 additions & 3 deletions src/com/goide/psi/impl/GoFieldNameReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public boolean execute(@NotNull PsiElement psiElement, @NotNull ResolveState res
}

@Nullable
private GoType getType(GoType type) { // todo: rethink and unify this algorithm
private GoType getType(@Nullable GoType type) { // todo: rethink and unify this algorithm
boolean inValue = myValue != null;

if (inValue && type instanceof GoArrayOrSliceType) type = ((GoArrayOrSliceType)type).getType();
else if (type instanceof GoMapType) type = inValue ? ((GoMapType)type).getValueType() : ((GoMapType)type).getKeyType();
else if (inValue && type instanceof GoStructType) {
else if (inValue && type instanceof GoSpecType && ((GoSpecType)type).getType() instanceof GoStructType) {
GoKey key = PsiTreeUtil.getPrevSiblingOfType(myValue, GoKey.class);
GoFieldName field = key != null ? key.getFieldName() : null;
PsiReference reference = field != null ? field.getReference() : null;
Expand All @@ -91,7 +91,7 @@ else if (inValue && type instanceof GoStructType) {
}
}

return type;
return type instanceof GoSpecType ? ((GoSpecType)type).getType() : type;
}

@Nullable
Expand Down
3 changes: 1 addition & 2 deletions src/com/goide/psi/impl/GoNamedElementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@

import javax.swing.*;

public abstract class GoNamedElementImpl<T extends GoNamedStub<?>> extends GoStubbedElementImpl<T>
implements GoCompositeElement, GoNamedElement {
public abstract class GoNamedElementImpl<T extends GoNamedStub<?>> extends GoStubbedElementImpl<T> implements GoCompositeElement, GoNamedElement {

public GoNamedElementImpl(@NotNull T stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
Expand Down
19 changes: 14 additions & 5 deletions src/com/goide/psi/impl/GoPsiImplUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ else if (o instanceof GoIndexOrSliceExpr) {
if (typeRef != null) {
type = getType(typeRef);
}
if (type instanceof GoSpecType) type = ((GoSpecType)type).getType();
if (type instanceof GoMapType) {
List<GoType> list = ((GoMapType)type).getTypeList();
if (list.size() == 2) {
Expand Down Expand Up @@ -411,7 +412,7 @@ public boolean value(GoTypeSpec spec) {
}
});
if (str != null) {
return str.getType();
return str.getSpecType(); // todo
}
}
return null;
Expand Down Expand Up @@ -507,7 +508,7 @@ public boolean shouldGoDeeper() {
public static GoType getType(@Nullable GoTypeReferenceExpression expression) {
PsiReference reference = expression != null ? expression.getReference() : null;
PsiElement resolve = reference != null ? reference.resolve() : null;
return resolve instanceof GoTypeSpec ? ((GoTypeSpec)resolve).getType() : null;
return resolve instanceof GoTypeSpec ? ((GoTypeSpec)resolve).getSpecType() : null;
}

public static boolean isVariadic(@NotNull GoParamDefinition o) {
Expand All @@ -522,7 +523,7 @@ public static boolean isVariadic(@NotNull GoParameterDeclaration o) {

@Nullable
public static GoType getGoTypeInner(@NotNull GoTypeSpec o, @SuppressWarnings("UnusedParameters") @Nullable ResolveState context) {
return o.getType();
return o.getSpecType();
}

@Nullable
Expand Down Expand Up @@ -565,7 +566,10 @@ private static GoType findTypeInVarSpec(@NotNull GoVarDefinition o, @Nullable Re
if (exprs.size() == 1 && exprs.get(0) instanceof GoCallExpr) {
GoExpression call = exprs.get(0);
GoType fromCall = call.getGoType(context);
GoType type = funcType(typeFromRefOrType(fromCall));
boolean canDecouple = varList.size() > 1;
GoType underlyingType = canDecouple && fromCall instanceof GoSpecType ? ((GoSpecType)fromCall).getType() : fromCall;
GoType byRef = typeFromRefOrType(underlyingType);
GoType type = funcType(canDecouple && byRef instanceof GoSpecType ? ((GoSpecType)byRef).getType() : byRef);
if (type == null) return fromCall;
if (type instanceof GoTypeList) {
if (((GoTypeList)type).getTypeList().size() > i) {
Expand Down Expand Up @@ -607,7 +611,7 @@ private static GoType processRangeClause(@NotNull GoVarDefinition o, @NotNull Go
if (typeRef != null) {
PsiElement resolve = typeRef.getReference().resolve();
if (resolve instanceof GoTypeSpec) {
type = ((GoTypeSpec)resolve).getType();
type = ((GoTypeSpec)resolve).getSpecType().getType();
if (type instanceof GoChannelType) {
return ((GoChannelType)type).getType();
}
Expand Down Expand Up @@ -680,6 +684,11 @@ public Result<List<GoMethodDeclaration>> compute() {
return calcMethods(o);
}

@NotNull
public PsiElement getType(@NotNull GoTypeSpec o) {
return o.getSpecType();
}

@NotNull
private static List<GoMethodDeclaration> calcMethods(@NotNull GoTypeSpec o) {
PsiFile file = o.getContainingFile().getOriginalFile();
Expand Down
Loading

0 comments on commit 44a078d

Please sign in to comment.