Skip to content

Commit 8d01b50

Browse files
committed
improve hover
1 parent 47d85bf commit 8d01b50

4 files changed

Lines changed: 231 additions & 56 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/HoverInfo.java

Lines changed: 85 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import de.peeeq.wurstio.languageserver.WFile;
66
import de.peeeq.wurstscript.WLogger;
77
import de.peeeq.wurstscript.ast.*;
8+
import de.peeeq.wurstscript.attributes.AttrWurstDoc;
89
import de.peeeq.wurstscript.attributes.names.FuncLink;
910
import de.peeeq.wurstscript.attributes.names.NameLink;
11+
import de.peeeq.wurstscript.parser.TriviaIndex;
1012
import de.peeeq.wurstscript.types.WurstType;
1113
import de.peeeq.wurstscript.types.WurstTypeNamedScope;
1214
import de.peeeq.wurstscript.utils.Utils;
@@ -18,6 +20,7 @@
1820
import java.util.ArrayList;
1921
import java.util.Collections;
2022
import java.util.List;
23+
import java.util.Optional;
2124

2225
/**
2326
* Created by peter on 24.04.16.
@@ -43,6 +46,11 @@ public Hover execute(ModelManager modelManager) {
4346
if (cu == null) {
4447
return new Hover(Collections.singletonList(Either.forLeft("File " + filename + " is not part of the project. Move it to the wurst folder.")));
4548
}
49+
int offset = offsetAt(line, column);
50+
Optional<TriviaIndex.CommentTrivia> commentTrivia = cu.getCuInfo().getTriviaIndex().findCommentAtOffset(offset);
51+
if (commentTrivia.isPresent()) {
52+
return new Hover(Collections.emptyList());
53+
}
4654
Element e = Utils.getAstElementAtPos(cu, line, column, false).get();
4755
WLogger.debug("hovering over " + Utils.printElement(e));
4856
List<Either<String, MarkedString>> desription = e.match(new Description());
@@ -51,6 +59,24 @@ public Hover execute(ModelManager modelManager) {
5159
return new Hover(desription);
5260
}
5361

62+
private int offsetAt(int line, int column) {
63+
int currentLine = 1;
64+
int currentColumn = 1;
65+
for (int i = 0; i < buffer.length(); i++) {
66+
if (currentLine == line && currentColumn == column) {
67+
return i;
68+
}
69+
char c = buffer.charAt(i);
70+
if (c == '\n') {
71+
currentLine++;
72+
currentColumn = 1;
73+
} else {
74+
currentColumn++;
75+
}
76+
}
77+
return Math.max(0, buffer.length() - 1);
78+
}
79+
5480
private List<Either<String, MarkedString>> addArgumentHint(Element e, List<Either<String, MarkedString>> desription) {
5581
try {
5682
if (e.getParent() instanceof Arguments) {
@@ -62,7 +88,7 @@ private List<Either<String, MarkedString>> addArgumentHint(Element e, List<Eithe
6288
if (f != null) {
6389
WurstType parameterType = f.getParameterType(index);
6490
String parameterName = f.getParameterName(index);
65-
desription = Utils.append(desription, Either.forLeft("Parameter " + parameterType + " " + parameterName));
91+
desription = Utils.append(desription, Either.forRight(new MarkedString("wurst", "parameter " + parameterType + " " + parameterName)));
6692
}
6793
}
6894
}
@@ -154,18 +180,22 @@ public List<Either<String, MarkedString>> description(NameDef n) {
154180
if (comment != null && !comment.isEmpty()) {
155181
result.add(Either.forLeft(comment));
156182
}
157-
if (n.attrIsConstant()) {
158-
if (n instanceof GlobalOrLocalVarDef) {
159-
GlobalOrLocalVarDef v = (GlobalOrLocalVarDef) n;
160-
VarInitialization initialExpr = v.getInitialExpr();
161-
String initial = Utils.prettyPrint(initialExpr);
162-
result.add(Either.forRight(new MarkedString("wurst", " = " + initial)));
183+
184+
String initializer = "";
185+
if (n instanceof GlobalOrLocalVarDef) {
186+
GlobalOrLocalVarDef v = (GlobalOrLocalVarDef) n;
187+
VarInitialization initialExpr = v.getInitialExpr();
188+
if (!(initialExpr instanceof NoExpr)) {
189+
initializer = " = " + Utils.prettyPrint(initialExpr);
163190
}
164191
}
165192

166-
String additionalProposalInfo = type(n.attrTyp()) + " " + n.getName()
167-
+ " defined in " + nearestScopeName(n);
168-
result.add(Either.forLeft(additionalProposalInfo));
193+
if (n instanceof TypeParamDef) {
194+
result.add(Either.forRight(new MarkedString("wurst", "type parameter " + n.getName())));
195+
} else {
196+
result.add(Either.forRight(new MarkedString("wurst", type(n.attrTyp()) + " " + n.getName() + initializer)));
197+
}
198+
result.add(Either.forLeft("defined in " + nearestScopeName(n)));
169199
return result;
170200
}
171201

@@ -266,10 +296,13 @@ public List<Either<String, MarkedString>> case_ExprCast(ExprCast e) {
266296

267297
@Override
268298
public List<Either<String, MarkedString>> case_WImport(WImport imp) {
299+
List<Either<String, MarkedString>> result = new ArrayList<>();
269300
WPackage imported = imp.attrImportedPackage();
270-
if (imported != null)
271-
return string(imported.attrComment());
272-
return string("import ...");
301+
if (imported != null && imported.attrComment() != null && !imported.attrComment().isEmpty()) {
302+
result.add(Either.forLeft(imported.attrComment()));
303+
}
304+
result.add(Either.forRight(new MarkedString("wurst", "import " + imp.getPackagename())));
305+
return result;
273306
}
274307

275308
@Override
@@ -303,31 +336,22 @@ public List<Either<String, MarkedString>> case_Annotation(Annotation annotation)
303336

304337
@Override
305338
public List<Either<String, MarkedString>> case_StmtExitwhen(StmtExitwhen stmtExitwhen) {
306-
return string("extiwhen: Exits the current loop when the condition is true");
339+
return string("exitwhen: exits the current loop when the condition is true.");
307340
}
308341

309342
@Override
310343
public List<Either<String, MarkedString>> case_ConstructorDef(ConstructorDef constr) {
311-
List<Either<String, MarkedString>> result = new ArrayList<>();
312-
NamedScope c = constr.attrNearestNamedScope();
313-
String comment = constr.attrComment();
314-
result.add(Either.forLeft(comment));
315-
316-
317-
String descr = "construct(" + getParameterString(constr) + ") "
318-
+ "defined in " + Utils.printElement(c);
319-
result.add(Either.forRight(new MarkedString("wurst", descr)));
320-
return result;
344+
return description(constr);
321345
}
322346

323347
@Override
324348
public List<Either<String, MarkedString>> case_WImports(WImports wImports) {
325-
return string("imports");
349+
return Collections.emptyList();
326350
}
327351

328352
@Override
329353
public List<Either<String, MarkedString>> case_WStatements(WStatements wStatements) {
330-
return string("statements");
354+
return Collections.emptyList();
331355
}
332356

333357
@Override
@@ -337,7 +361,7 @@ public List<Either<String, MarkedString>> case_CompilationUnit(CompilationUnit c
337361

338362
@Override
339363
public List<Either<String, MarkedString>> case_SwitchStmt(SwitchStmt switchStmt) {
340-
return string("A switch statement does different things depending on the value of an epxression.");
364+
return string("A switch statement executes branches based on an expression value.");
341365
}
342366

343367
@Override
@@ -373,7 +397,7 @@ public List<Either<String, MarkedString>> case_SomeSuperConstructorCall(SomeSupe
373397

374398
@Override
375399
public List<Either<String, MarkedString>> case_LocalVarDef(LocalVarDef v) {
376-
return string("Local Variable " + v.getName() + " of type " + type(v.attrTyp()));
400+
return description(v);
377401
}
378402

379403
private List<Either<String, MarkedString>> string(String s) {
@@ -467,12 +491,12 @@ public List<Either<String, MarkedString>> case_ExprTypeId(ExprTypeId exprTypeId)
467491

468492
@Override
469493
public List<Either<String, MarkedString>> case_TypeExprList(TypeExprList typeExprList) {
470-
return string("A list of type-expressions");
494+
return Collections.emptyList();
471495
}
472496

473497
@Override
474498
public List<Either<String, MarkedString>> case_FuncDefs(FuncDefs funcDefs) {
475-
return string("A list of function definitions");
499+
return Collections.emptyList();
476500
}
477501

478502
@Override
@@ -496,22 +520,22 @@ public List<Either<String, MarkedString>> case_VisibilityDefault(VisibilityDefau
496520

497521
@Override
498522
public List<Either<String, MarkedString>> case_Arguments(Arguments arguments) {
499-
return string("List of arguments");
523+
return Collections.emptyList();
500524
}
501525

502526
@Override
503527
public List<Either<String, MarkedString>> case_ModuleInstanciations(ModuleInstanciations moduleInstanciations) {
504-
return string("List of module instantiations.");
528+
return Collections.emptyList();
505529
}
506530

507531
@Override
508532
public List<Either<String, MarkedString>> case_WShortParameters(WShortParameters wShortParameters) {
509-
return string("Parameters of anonymous function.");
533+
return Collections.emptyList();
510534
}
511535

512536
@Override
513537
public List<Either<String, MarkedString>> case_SwitchDefaultCaseStatements(SwitchDefaultCaseStatements switchDefaultCaseStatements) {
514-
return string("Default statements of switch-statement");
538+
return Collections.emptyList();
515539
}
516540

517541
@Override
@@ -521,7 +545,7 @@ public List<Either<String, MarkedString>> case_ExprStatementsBlock(ExprStatement
521545

522546
@Override
523547
public List<Either<String, MarkedString>> case_ModuleUses(ModuleUses moduleUses) {
524-
return string("A list of module uses");
548+
return Collections.emptyList();
525549
}
526550

527551
@Override
@@ -531,7 +555,7 @@ public List<Either<String, MarkedString>> case_GlobalVarDef(GlobalVarDef globalV
531555

532556
@Override
533557
public List<Either<String, MarkedString>> case_JassToplevelDeclarations(JassToplevelDeclarations jassToplevelDeclarations) {
534-
return string("A list of declarations.");
558+
return Collections.emptyList();
535559
}
536560

537561
@Override
@@ -546,19 +570,23 @@ public List<Either<String, MarkedString>> case_ExprMemberArrayVarDotDot(ExprMemb
546570

547571
@Override
548572
public List<Either<String, MarkedString>> case_ConstructorDefs(ConstructorDefs constructorDefs) {
549-
return string("A list of constructors");
573+
return Collections.emptyList();
550574
}
551575

552576
private List<Either<String, MarkedString>> typeExpr(TypeExpr t) {
577+
NameDef nameDef = t.tryGetNameDef();
578+
if (nameDef != null) {
579+
return description(nameDef);
580+
}
553581
WurstType wt = t.attrTyp();
554582
if (wt == null) {
555-
return string("Type " + t);
583+
return Collections.singletonList(Either.forRight(new MarkedString("wurst", "type " + t)));
556584
}
557585
if (wt instanceof WurstTypeNamedScope) {
558586
WurstTypeNamedScope wtn = (WurstTypeNamedScope) wt;
559587
return description(wtn.getDef());
560588
}
561-
return string(type(wt));
589+
return Collections.singletonList(Either.forRight(new MarkedString("wurst", type(wt))));
562590
}
563591

564592
@Override
@@ -574,7 +602,7 @@ public List<Either<String, MarkedString>> case_TypeExprSimple(TypeExprSimple t)
574602

575603
@Override
576604
public List<Either<String, MarkedString>> case_Modifiers(Modifiers modifiers) {
577-
return string("A list of modifiers");
605+
return string("Modifiers for this declaration.");
578606
}
579607

580608
@Override
@@ -599,7 +627,7 @@ public List<Either<String, MarkedString>> case_FuncDef(FuncDef funcDef) {
599627

600628
@Override
601629
public List<Either<String, MarkedString>> case_ExprList(ExprList exprList) {
602-
return string("A list of expressions.");
630+
return Collections.emptyList();
603631
}
604632

605633
@Override
@@ -659,17 +687,17 @@ public List<Either<String, MarkedString>> case_ExprFuncRef(ExprFuncRef exprFuncR
659687

660688
@Override
661689
public List<Either<String, MarkedString>> case_TypeParamDefs(TypeParamDefs typeParamDefs) {
662-
return string("A list of type parameters.");
690+
return Collections.emptyList();
663691
}
664692

665693
@Override
666694
public List<Either<String, MarkedString>> case_StmtForFrom(StmtForFrom stmtForFrom) {
667-
return string("The for-from loop takes an iterate and takes elements from the iterator until it is empty.");
695+
return string("The for-from loop repeatedly takes elements from an iterator until it is empty.");
668696
}
669697

670698
@Override
671699
public List<Either<String, MarkedString>> case_Indexes(Indexes indexes) {
672-
return string("A list of indexes");
700+
return Collections.emptyList();
673701
}
674702

675703
@Override
@@ -719,7 +747,7 @@ public List<Either<String, MarkedString>> case_IdentifierWithTypeParamDefs(Ident
719747

720748
@Override
721749
public List<Either<String, MarkedString>> case_GlobalVarDefs(GlobalVarDefs globalVarDefs) {
722-
return string("A list of global variables.");
750+
return Collections.emptyList();
723751
}
724752

725753
@Override
@@ -734,7 +762,7 @@ public List<Either<String, MarkedString>> case_StmtReturn(StmtReturn stmtReturn)
734762

735763
@Override
736764
public List<Either<String, MarkedString>> case_WPackages(WPackages wPackages) {
737-
return string("A list of packages.");
765+
return Collections.emptyList();
738766
}
739767

740768
@Override
@@ -744,7 +772,7 @@ public List<Either<String, MarkedString>> case_ExprIfElse(ExprIfElse exprIfElse)
744772

745773
@Override
746774
public List<Either<String, MarkedString>> case_WurstDoc(WurstDoc wurstDoc) {
747-
return wurstDoc.getParent().match(this);
775+
return string(AttrWurstDoc.normalizeHotdocComment(wurstDoc.getRawComment()));
748776
}
749777

750778
@Override
@@ -769,7 +797,7 @@ public List<Either<String, MarkedString>> case_OnDestroyDef(OnDestroyDef onDestr
769797

770798
@Override
771799
public List<Either<String, MarkedString>> case_ModVararg(ModVararg modVararg) {
772-
return string("Declares the parameter to be a array of variable length");
800+
return string("Declares this parameter as a variable-length argument list.");
773801
}
774802

775803
@Override
@@ -784,7 +812,7 @@ public List<Either<String, MarkedString>> case_VisibilityPublic(VisibilityPublic
784812

785813
@Override
786814
public List<Either<String, MarkedString>> case_TopLevelDeclarations(TopLevelDeclarations topLevelDeclarations) {
787-
return string("A list of declarations.");
815+
return Collections.emptyList();
788816
}
789817

790818
@Override
@@ -799,12 +827,12 @@ public List<Either<String, MarkedString>> case_ExprDestroy(ExprDestroy exprDestr
799827

800828
@Override
801829
public List<Either<String, MarkedString>> case_WEntities(WEntities wEntities) {
802-
return string("A list of entities");
830+
return Collections.emptyList();
803831
}
804832

805833
@Override
806834
public List<Either<String, MarkedString>> case_ArraySizes(ArraySizes arraySizes) {
807-
return string("A list of array-sizes");
835+
return Collections.emptyList();
808836
}
809837

810838
@Override
@@ -819,12 +847,15 @@ public List<Either<String, MarkedString>> case_SwitchCase(SwitchCase switchCase)
819847

820848
@Override
821849
public List<Either<String, MarkedString>> case_EnumMembers(EnumMembers enumMembers) {
822-
return string("A list of enum-members.");
850+
return Collections.emptyList();
823851
}
824852

825853
@Override
826854
public List<Either<String, MarkedString>> case_TypeExprThis(TypeExprThis typeExprThis) {
827-
return typeExpr(typeExprThis);
855+
List<Either<String, MarkedString>> result = new ArrayList<>();
856+
result.add(Either.forLeft("'thistype' refers to the dynamic type of 'this' in the current context."));
857+
result.add(Either.forRight(new MarkedString("wurst", "resolved as " + type(typeExprThis.attrTyp()))));
858+
return result;
828859
}
829860

830861
@Override
@@ -864,7 +895,7 @@ public List<Either<String, MarkedString>> case_ExprMemberVarDot(ExprMemberVarDot
864895

865896
@Override
866897
public List<Either<String, MarkedString>> case_WParameters(WParameters wParameters) {
867-
return string("A list of parameters");
898+
return Collections.emptyList();
868899
}
869900
}
870901

0 commit comments

Comments
 (0)