Skip to content

Commit

Permalink
Merge pull request #598 from WebFreak001/skip-private-complete
Browse files Browse the repository at this point in the history
make private symbols not show up in auto complete
merged-on-behalf-of: Basile-z <Basile-z@users.noreply.github.com>
  • Loading branch information
dlang-bot authored May 11, 2019
2 parents 82432e1 + 6a195ea commit afb2113
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dsymbol
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"license": "GPL-3.0",
"dependencies": {
"dsymbol": "~>0.6.4",
"dsymbol": "~>0.7.0",
"libdparse": "~>0.11.4",
"msgpack-d": "~>1.0.0-beta.7",
"stdx-allocator": "~>2.77.5",
Expand Down
30 changes: 24 additions & 6 deletions src/dcd/server/autocomplete/complete.d
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ void setCompletions(T)(ref AutocompleteResponse response,
CompletionType completionType, bool isBracket = false, string partial = null)
{
static void addSymToResponse(const(DSymbol)* s, ref AutocompleteResponse r, string p,
size_t[] circularGuard = [])
Scope* completionScope, size_t[] circularGuard = [])
{
if (circularGuard.canFind(cast(size_t) s))
return;
Expand All @@ -512,12 +512,13 @@ void setCompletions(T)(ref AutocompleteResponse response,
if (sym.name !is null && sym.name.length > 0 && isPublicCompletionKind(sym.kind)
&& (p is null ? true : toUpper(sym.name.data).startsWith(toUpper(p)))
&& !r.completions.canFind!(a => a.identifier == sym.name)
&& sym.name[0] != '*')
&& sym.name[0] != '*'
&& mightBeRelevantInCompletionScope(sym, completionScope))
{
r.completions ~= makeSymbolCompletionInfo(sym, sym.kind);
}
if (sym.kind == CompletionKind.importSymbol && !sym.skipOver && sym.type !is null)
addSymToResponse(sym.type, r, p, circularGuard ~ (cast(size_t) s));
addSymToResponse(sym.type, r, p, completionScope, circularGuard ~ (cast(size_t) s));
}
}

Expand All @@ -527,7 +528,8 @@ void setCompletions(T)(ref AutocompleteResponse response,
{
auto currentSymbols = completionScope.getSymbolsInCursorScope(cursorPosition);
foreach (s; currentSymbols.filter!(a => isPublicCompletionKind(a.kind)
&& toUpper(a.name.data).startsWith(toUpper(partial))))
&& toUpper(a.name.data).startsWith(toUpper(partial))
&& mightBeRelevantInCompletionScope(a, completionScope)))
{
response.completions ~= makeSymbolCompletionInfo(s, s.kind);
}
Expand All @@ -545,7 +547,8 @@ void setCompletions(T)(ref AutocompleteResponse response,
&& a.kind != CompletionKind.dummy
&& a.symbolFile == "stdin"
&& (partial !is null && toUpper(a.name.data).startsWith(toUpper(partial))
|| partial is null)))
|| partial is null)
&& mightBeRelevantInCompletionScope(a, completionScope)))
{
response.completions ~= makeSymbolCompletionInfo(s, s.kind);
}
Expand Down Expand Up @@ -574,7 +577,7 @@ void setCompletions(T)(ref AutocompleteResponse response,
if (symbols.length == 0)
return;
}
addSymToResponse(symbols[0], response, partial);
addSymToResponse(symbols[0], response, partial, completionScope);
response.completionType = CompletionType.identifiers;
}
else if (completionType == CompletionType.calltips)
Expand Down Expand Up @@ -651,6 +654,21 @@ void setCompletions(T)(ref AutocompleteResponse response,
}
}

bool mightBeRelevantInCompletionScope(const DSymbol* symbol, Scope* scope_)
{
import dparse.lexer : tok;

if (symbol.protection == tok!"private" &&
!scope_.hasSymbolRecursive(symbol))
{
// scope is the scope of the current file so if the symbol is not in there, it's not accessible
return false;
}

return true;
}


AutocompleteResponse.Completion generateStructConstructorCalltip(const DSymbol* symbol)
in
{
Expand Down
14 changes: 14 additions & 0 deletions tests/tc_access_modifiers/bar.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module tc_access_modifiers.bar;

class Helper
{
private int mfieldPrivate;
protected int mfieldProtected;
package int mfieldPackage;
int mfieldPublic;

private void mfuncPrivate() {}
public void mfuncPublic() {}
private static void mfuncPrivateStatic() {}
public static void mfuncPublicStatic() {}
}
Empty file.
4 changes: 4 additions & 0 deletions tests/tc_access_modifiers/expected1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
identifiers
mfieldPackage v
mfieldProtected v
mfieldPublic v
3 changes: 3 additions & 0 deletions tests/tc_access_modifiers/expected1_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
identifiers
mfuncPublic f
mfuncPublicStatic f
3 changes: 3 additions & 0 deletions tests/tc_access_modifiers/expected2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
identifiers
myprivate v
mypublic v
4 changes: 4 additions & 0 deletions tests/tc_access_modifiers/expected3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
identifiers
mfieldPackage v
mfieldProtected v
mfieldPublic v
16 changes: 16 additions & 0 deletions tests/tc_access_modifiers/file1.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import tc_access_modifiers.bar;

struct X
{
public int mypublic;
private int myprivate;
}

void main()
{
Helper helper;
helper.mfield;
helper.mfunc;
X foo;
foo.myp;
}
9 changes: 9 additions & 0 deletions tests/tc_access_modifiers/file2.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import tc_access_modifiers.bar;

class Subclass : Helper
{
this()
{
this.mfield
}
}
26 changes: 26 additions & 0 deletions tests/tc_access_modifiers/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
set -e
set -u

rm -f actual1.txt actual2.txt actual3.txt actual0.txt

../../bin/dcd-client $1 file1.d -c 137 | sed s\""$(dirname "$(pwd)")"\"\" > actual0.txt
diff actual0.txt expected0.txt

../../bin/dcd-client $1 -I bar.d

../../bin/dcd-client $1 file1.d -c 137 | sed s\""$(dirname "$(pwd)")"\"\" > actual1.txt
diff actual1.txt expected1.txt

../../bin/dcd-client $1 file1.d -c 152 | sed s\""$(dirname "$(pwd)")"\"\" > actual1_1.txt
diff actual1_1.txt expected1_1.txt

../../bin/dcd-client $1 file1.d -c 170 | sed s\""$(dirname "$(pwd)")"\"\" > actual2.txt
diff actual2.txt expected2.txt

../../bin/dcd-client $1 file2.d -c 83 | sed s\""$(dirname "$(pwd)")"\"\" > actual3.txt
diff actual3.txt expected3.txt

../../bin/dcd-client $1 -R bar.d

../../bin/dcd-client $1 file1.d -c 137 | sed s\""$(dirname "$(pwd)")"\"\" > actual0.txt
diff actual0.txt expected0.txt

0 comments on commit afb2113

Please sign in to comment.