Skip to content

Commit 3bebd15

Browse files
authored
Merge pull request #1 from vushu/applied_more_test_cases_and_handling_life_time
Updated tests added return info update
2 parents 57c5299 + a3955b0 commit 3bebd15

10 files changed

Lines changed: 132 additions & 33 deletions

File tree

dsymbol/src/dsymbol/conversion/first.d

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ final class FirstPass : ASTVisitor
132132
currentSymbol.acSymbol.protection = protection.current;
133133
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);
134134
currentSymbol.acSymbol.qualifier = SymbolQualifier.func;
135+
foreach (sc; dec.storageClasses)
136+
{
137+
if (sc.token.type == tok!"ref")
138+
{
139+
currentSymbol.acSymbol.returnIsRef = true;
140+
break;
141+
}
142+
}
135143

136144
istring lastComment = this.lastComment;
137145
this.lastComment = istring.init;
@@ -172,6 +180,11 @@ final class FirstPass : ASTVisitor
172180
block.startLocation, null);
173181
scope(exit) popSymbol();
174182

183+
if (exp.returnRefType == ReturnRefType.ref_)
184+
{
185+
currentSymbol.acSymbol.returnIsRef = true;
186+
}
187+
175188
pushScope(block.startLocation, block.endLocation);
176189
scope (exit) popScope();
177190
processParameters(currentSymbol, exp.returnType,

dsymbol/src/dsymbol/symbol.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ struct DSymbol
443443
bool, "_flag8", 1,
444444
bool, "_flag9", 1,
445445
bool, "_flag10", 1,
446-
uint, "", 3,
446+
bool, "_flag11", 1,
447+
uint, "", 2,
447448
));
448449
// dfmt on
449450

@@ -463,6 +464,8 @@ struct DSymbol
463464
alias parameterIsOut = _flag9;
464465
/// Only valid for parameters: the parameter has storage class `in`
465466
alias parameterIsIn = _flag10;
467+
/// If the return type is a ref
468+
alias returnIsRef = _flag11;
466469

467470
deprecated bool isPointer()
468471
{

dsymbol/src/dsymbol/ufcs.d

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ import std.string;
1515
import std.regex;
1616
import containers.hashset : HashSet;
1717
import std.experimental.logger;
18-
1918
alias SortedTokens = SortedRange!(const(Token)[], "a < b");
2019

20+
struct ExpressionInfo
21+
{
22+
const(DSymbol)* type;
23+
bool assumingLvalue; // We only assume else we need to do life time analysis.
24+
bool isFromFunction;
25+
string name;
26+
}
27+
2128
enum CompletionContext
2229
{
2330
UnknownCompletion,
@@ -128,8 +135,8 @@ private const(DSymbol)* deduceExpressionType(
128135
symbolNameToTypeName(STRING_LITERAL_SYMBOL_NAME), cursorPosition);
129136
}
130137

131-
auto currentType =
132-
deduceSymbolTypeByToken(completionScope, *firstToken, cursorPosition);
138+
auto currentType = deduceSymbolTypeByToken(completionScope, *firstToken, cursorPosition);
139+
133140

134141
if (currentType is null)
135142
return null;
@@ -174,8 +181,18 @@ private const(DSymbol)* deduceExpressionType(
174181
auto name = istring(exprTokens[i + 1].text);
175182

176183
// Get UFCS candidates for current type
184+
ExpressionInfo beforeDotType;
185+
beforeDotType.type = currentType;
186+
beforeDotType.assumingLvalue = false;
187+
// check if there it's from a function
188+
auto functionSymbol = completionScope.getFirstSymbolByNameAndCursor(istring(firstToken.text), cursorPosition);
189+
if (functionSymbol) {
190+
beforeDotType.isFromFunction = functionSymbol.qualifier == SymbolQualifier.func;
191+
beforeDotType.name = functionSymbol.name;
192+
}
193+
177194
auto candidates = getUFCSSymbolsForDotCompletion(
178-
currentType,
195+
beforeDotType,
179196
completionScope,
180197
cursorPosition,
181198
""
@@ -207,23 +224,6 @@ private const(DSymbol)* deduceExpressionType(
207224
return currentType;
208225
}
209226

210-
void printTokenType(const(Token)* token)
211-
{
212-
if (token is null)
213-
{
214-
return;
215-
}
216-
switch (token.type)
217-
{
218-
case tok!"":
219-
220-
break;
221-
222-
default:
223-
break;
224-
}
225-
}
226-
227227
private const(DSymbol)* deduceSymbolTypeByToken(Scope* completionScope, scope ref const(Token) significantToken, size_t cursorPosition)
228228
{
229229

@@ -429,14 +429,15 @@ DSymbol*[] getUFCSSymbolsForCursor(Scope* completionScope, scope ref const(Token
429429
return [];
430430
}
431431

432-
const(DSymbol)* deducedSymbolType = deduceExpressionType(completionScope, tokenCursorResult.expressionTokens, cursorPosition);
432+
ExpressionInfo deducedSymbolType;
433+
deducedSymbolType.type = deduceExpressionType(completionScope, tokenCursorResult.expressionTokens, cursorPosition);
433434

434-
if (deducedSymbolType is null)
435+
if (deducedSymbolType.type is null)
435436
{
436437
return [];
437438
}
438439

439-
if (deducedSymbolType.isInvalidForUFCSCompletion)
440+
if (deducedSymbolType.type.isInvalidForUFCSCompletion)
440441
{
441442
trace("CursorSymbolType isn't valid for UFCS completion");
442443
return [];
@@ -454,7 +455,7 @@ DSymbol*[] getUFCSSymbolsForCursor(Scope* completionScope, scope ref const(Token
454455

455456
}
456457

457-
private DSymbol*[] getUFCSSymbolsForDotCompletion(const(DSymbol)* symbolType, Scope* completionScope, size_t cursorPosition, string partial)
458+
private DSymbol*[] getUFCSSymbolsForDotCompletion(ExpressionInfo symbolType, Scope* completionScope, size_t cursorPosition, string partial)
458459
{
459460
// local appender
460461
FilteredAppender!((DSymbol* a) =>
@@ -472,7 +473,7 @@ private DSymbol*[] getUFCSSymbolsForDotCompletion(const(DSymbol)* symbolType, Sc
472473
return localAppender.data ~ globalAppender.data;
473474
}
474475

475-
private DSymbol*[] getUFCSSymbolsForParenCompletion(const(DSymbol)* symbolType, Scope* completionScope, istring searchWord, size_t cursorPosition)
476+
private DSymbol*[] getUFCSSymbolsForParenCompletion(ExpressionInfo symbolType, Scope* completionScope, istring searchWord, size_t cursorPosition)
476477
{
477478
// local appender
478479
FilteredAppender!(a => a.isCallableWithArg(symbolType) && a.name.among(searchWord), DSymbol*[]) localAppender;
@@ -644,10 +645,10 @@ private bool matchSymbolType(const(DSymbol)* firstParameter, const(DSymbol)* sig
644645
* `true` if `incomingSymbols`' first parameter matches `beforeDotType`
645646
* `false` otherwise
646647
*/
647-
bool isCallableWithArg(const(DSymbol)* incomingSymbol, const(DSymbol)* beforeDotType, bool isGlobalScope = false)
648+
bool isCallableWithArg(const(DSymbol)* incomingSymbol, ExpressionInfo beforeDotType, bool isGlobalScope = false)
648649
{
649650
if (incomingSymbol is null
650-
|| beforeDotType is null
651+
|| beforeDotType.type is null
651652
|| isGlobalScope && incomingSymbol.protection is tok!"private") // don't show private functions if we are in global scope
652653
{
653654
return false;
@@ -656,7 +657,8 @@ bool isCallableWithArg(const(DSymbol)* incomingSymbol, const(DSymbol)* beforeDot
656657
if (incomingSymbol.kind is CompletionKind.functionName && !incomingSymbol.functionParameters.empty && incomingSymbol
657658
.functionParameters.front.type)
658659
{
659-
return matchSymbolType(incomingSymbol.functionParameters.front, beforeDotType);
660+
auto firstParam = incomingSymbol.functionParameters.front;
661+
return matchSymbolType(firstParam, beforeDotType.type);
660662
}
661663
return false;
662664
}

tests/tc_ufcs_function_chaining_completion/expected_completion_test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ baz F
55
foo F
66
init k
77
mangleof k
8+
qux F
9+
refFoo F
10+
refFoo2 F
811
sizeof k
912
stringof k
1013
tupleof k
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
identifiers
2+
bar F
3+
baz F
4+
foo F
5+
qux F
6+
refFoo F
7+
refFoo2 F
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
identifiers
2+
bar F
3+
baz F
4+
foo F
5+
qux F
6+
refFoo F
7+
refFoo2 F
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
identifiers
2+
bar F
3+
baz F
4+
foo F
5+
qux F
6+
refFoo F
7+
refFoo2 F
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
identifiers
2+
alignof k
3+
bar F
4+
baz F
5+
foo F
6+
init k
7+
mangleof k
8+
qux F
9+
refFoo F
10+
refFoo2 F
11+
sizeof k
12+
stringof k
13+
tupleof k

tests/tc_ufcs_function_chaining_completion/file.d

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,41 @@ Foo baz(Foo f) {
1111
return f;
1212
}
1313

14+
Foo qux(Foo f) {
15+
return f;
16+
}
17+
18+
ref refFoo(ref Foo f) {
19+
return f;
20+
}
21+
22+
ref refFoo2(ref Foo f) {
23+
return f;
24+
}
1425

1526
void main()
1627
{
1728
Foo f;
1829
Foo foo = baz(f.foo().bar()).
19-
}
30+
}
31+
32+
void another() {
33+
Foo f;
34+
Foo foo = f.foo().bar().baz().
35+
}
36+
37+
void yetAnother() {
38+
Foo f;
39+
Foo foo = f.foo.bar().baz.qux.
40+
}
41+
42+
void justAnother() {
43+
Foo f;
44+
Foo foo = f.foo().baz().qux()
45+
.
46+
}
47+
48+
void refTest() {
49+
Foo f;
50+
f.
51+
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
set -e
22
set -u
33

4-
../../bin/dcd-client $1 -c158 file.d > actual_completion_test.txt
5-
diff actual_completion_test.txt expected_completion_test.txt
4+
../../bin/dcd-client $1 -c265 file.d > actual_completion_test.txt
5+
diff actual_completion_test.txt expected_completion_test.txt --strip-trailing-cr
6+
7+
../../bin/dcd-client $1 -c325 file.d > actual_completion_test2.txt
8+
diff actual_completion_test2.txt expected_completion_test2.txt --strip-trailing-cr
9+
10+
../../bin/dcd-client $1 -c388 file.d > actual_completion_test3.txt
11+
diff actual_completion_test3.txt expected_completion_test3.txt --strip-trailing-cr
12+
13+
../../bin/dcd-client $1 -c454 file.d > actual_completion_test4.txt
14+
diff actual_completion_test4.txt expected_completion_test4.txt --strip-trailing-cr
15+
16+
../../bin/dcd-client $1 -c486 file.d > actual_completion_test5.txt
17+
diff actual_completion_test5.txt expected_completion_test5.txt --strip-trailing-cr

0 commit comments

Comments
 (0)