Skip to content

Commit 22f65d5

Browse files
committed
Resolve ptr, array & aa in types, add typeOf field
typeOf field is new tab-delimited field in dcd-client output, so you no longer need to manually guess types / parse code. Calltips still yield the actual written type, but in case of "auto", the typeOf column may contain more useful info.
1 parent 690d625 commit 22f65d5

File tree

19 files changed

+151
-21
lines changed

19 files changed

+151
-21
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,19 @@ a tab separated format:
175175
* definition: function or variable definition string or close approximation for information display purpose
176176
* symbol location: in which file (or `stdin`) & byte offset this symbol is defined. Separated with a space.
177177
* documentation: escaped documentation string of this symbol
178+
* typeOf: resolved type name of this symbol:
179+
<!-- the items in list are copied from messages.d -->
180+
* For variables, fields, globals, constants: resolved type or empty if unresolved.
181+
* For functions: resolved return type or empty if unresolved.
182+
* For constructors: may be struct/class name or empty in any case.
183+
* Otherwise (probably) empty.
178184

179185
#### Example `--extended` output
180186

181187
identifiers
182188
libraryFunction f Tuple!long libraryFunction(string s, string s2) stdin 190 foobar
183-
libraryFunction f int* libraryFunction(string s) stdin 99 Hello\nWorld
184-
libraryVariable v int libraryVariable stdin 56 My variable
189+
libraryFunction f int* libraryFunction(string s) stdin 99 Hello\nWorld int*
190+
libraryVariable v int libraryVariable stdin 56 My variable int
185191
libreTypes g stdin 298
186192

187193
#### Note
@@ -190,6 +196,9 @@ DCD's output will start with "identifiers" when completing at a left paren
190196
character if the keywords *pragma*, *scope*, *__traits*, *extern*, or *version*
191197
were just before the paren.
192198

199+
Types in the calltips and typeOf column may not be complete, e.g. missing
200+
template parameters or typeof expressions, etc.
201+
193202
### Parenthesis completion
194203

195204
When the first line of output is "calltips", the editor should display a function

common/src/dcd/common/messages.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ struct AutocompleteResponse
152152
* Documentation associated with this symbol.
153153
*/
154154
string documentation;
155+
// when changing the behavior here, update README.md
156+
/**
157+
* For variables, fields, globals, constants: resolved type or empty if unresolved.
158+
* For functions: resolved return type or empty if unresolved.
159+
* For constructors: may be struct/class name or empty in any case.
160+
* Otherwise (probably) empty.
161+
*/
162+
string typeOf;
155163
}
156164

157165
/**

dsymbol/src/dsymbol/symbol.d

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,43 @@ struct DSymbol
445445
/// Protection level for this symbol
446446
IdType protection;
447447

448+
string formatType(string suffix = "") const
449+
{
450+
if (kind == CompletionKind.functionName)
451+
{
452+
if (type) // try to give return type symbol
453+
return type.formatType;
454+
else // null if unresolved, user can manually pick .name or .callTip if needed
455+
return null;
456+
}
457+
else if (name == POINTER_SYMBOL_NAME)
458+
{
459+
if (!type)
460+
return suffix ~ "*";
461+
else
462+
return type.formatType(suffix ~ "*");
463+
}
464+
else if (name == ARRAY_SYMBOL_NAME)
465+
{
466+
if (!type)
467+
return suffix ~ "[]";
468+
else
469+
return type.formatType(suffix ~ "[]");
470+
}
471+
else if (name == ASSOC_ARRAY_SYMBOL_NAME)
472+
{
473+
// TODO: include AA key type
474+
if (!type)
475+
return suffix ~ "[...]";
476+
else
477+
return type.formatType(suffix ~ "[...]");
478+
}
479+
else
480+
{
481+
// TODO: include template parameters
482+
return name ~ suffix;
483+
}
484+
}
448485
}
449486

450487
/**

dsymbol/src/dsymbol/tests.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ unittest
217217
};
218218
ScopeSymbolPair pair = generateAutocompleteTrees(source, cache);
219219
DSymbol* meaningOfLife = pair.symbol.getFirstPartNamed(istring("meaningOfLife"));
220-
writeln(meaningOfLife.type.name);
221-
assert(meaningOfLife.type.name == "int");
220+
writeln(meaningOfLife.type.formatType);
221+
assert(meaningOfLife.type.formatType == "int*");
222222
}
223223

224224

src/dcd/client/client.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ void printCompletionResponse(ref const AutocompleteResponse response, bool exten
396396
completion.kind == char.init ? "" : "" ~ completion.kind,
397397
completion.definition,
398398
completion.symbolFilePath.length ? completion.symbolFilePath ~ " " ~ completion.symbolLocation.to!string : "",
399-
completion.documentation
399+
completion.documentation,
400+
completion.typeOf
400401
));
401402
else
402403
app.put(makeTabSeparated(completion.identifier, "" ~ completion.kind));

src/dcd/server/autocomplete/complete.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,5 +701,6 @@ do
701701
auto completion = makeSymbolCompletionInfo(symbol, char.init);
702702
completion.identifier = "this";
703703
completion.definition = generatedStructConstructorCalltip;
704+
completion.typeOf = symbol.name;
704705
return completion;
705706
}

src/dcd/server/autocomplete/util.d

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,16 +614,26 @@ bool isUdaExpression(T)(ref T tokens)
614614

615615
AutocompleteResponse.Completion makeSymbolCompletionInfo(const DSymbol* symbol, char kind)
616616
{
617-
string definition;
617+
auto ret = AutocompleteResponse.Completion(symbol.name, kind, null,
618+
symbol.symbolFile, symbol.location, symbol.doc);
619+
620+
if (symbol.type)
621+
ret.typeOf = symbol.type.formatType;
622+
618623
if ((kind == CompletionKind.variableName || kind == CompletionKind.memberVariableName) && symbol.type)
619-
definition = symbol.type.name ~ ' ' ~ symbol.name;
624+
{
625+
if (symbol.type.kind == CompletionKind.functionName && !ret.typeOf.length)
626+
ret.definition = symbol.type.name ~ ' ' ~ symbol.name;
627+
else
628+
ret.definition = ret.typeOf ~ ' ' ~ symbol.name;
629+
}
620630
else if (kind == CompletionKind.enumMember)
621-
definition = symbol.name; // TODO: add enum value to definition string
631+
ret.definition = symbol.name; // TODO: add enum value to definition string
622632
else
623-
definition = symbol.callTip;
624-
// TODO: definition strings could include more information, like on classes inheritance
625-
return AutocompleteResponse.Completion(symbol.name, kind, definition,
626-
symbol.symbolFile, symbol.location, symbol.doc);
633+
ret.definition = symbol.callTip;
634+
635+
// TODO: extend completion with more info such as class inheritance
636+
return ret;
627637
}
628638

629639
bool doUFCSSearch(string beforeToken, string lastToken)

tests/tc059/expected1.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
identifiers
2-
libraryFunction f Tuple!long libraryFunction(string s, string s2) stdin 190 foobar
3-
libraryFunction f int* libraryFunction(string s) stdin 99 Hello\nWorld
4-
libraryVariable v int libraryVariable stdin 56 My variable
2+
libraryFunction f Tuple!long libraryFunction(string s, string s2) stdin 223 foobar
3+
libraryFunction f int* libraryFunction(string s) stdin 132 Hello\nWorld int*
4+
libraryVariable v int libraryVariable stdin 56 My variable int
5+
libraryVariable2 v int* libraryVariable2 stdin 88 My variable int*

tests/tc059/file.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ void main(string[] args)
55

66
/// My variable
77
int libraryVariable;
8+
/// ditto
9+
int* libraryVariable2;
810

911
/// Hello
1012
/// World

tests/tc061/expected1.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
calltips
2-
libraryFunction Tuple!long libraryFunction(string s, string s2) stdin 166 foobar
3-
libraryFunction int* libraryFunction(string s) stdin 75 Hello\nWorld
2+
libraryFunction Tuple!long libraryFunction(string s, string s2) stdin 166 foobar
3+
libraryFunction int* libraryFunction(string s) stdin 75 Hello\nWorld int*

0 commit comments

Comments
 (0)