Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedupe code completions in components #664

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,40 @@ describe('Program', () => {
).to.eql(['sayHello', 'sayHello2', 'sayHello3', 'sayHello4']);
});

it('gets completions for callfunc invocation with multiple nodes and validates single code completion results', () => {
program.setFile('source/main.bs', `
function main()
ParentNode@.sayHello(arg1)
end function
`);
program.setFile('components/ParentNode.bs', `
function sayHello(text, text2)
end function
`);
program.setFile<XmlFile>('components/ParentNode.xml',
trim`<?xml version="1.0" encoding="utf-8" ?>
<component name="ParentNode" extends="Scene">
<script type="text/brightscript" uri="pkg:/components/ParentNode.bs" />
<interface>
<function name="sayHello"/>
</interface>
</component>`);
program.setFile('components/ChildNode.bs', `
function sayHello(text, text2)
end function
`);
program.setFile<XmlFile>('components/ChildNode.xml',
trim`<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildNode" extends="ParentNode">
<script type="text/brightscript" uri="pkg:/components/ChildNode.bs" />
</component>`);
program.validate();

expect(
(program.getCompletions(`${rootDir}/source/main.bs`, Position.create(2, 30))).map(x => x.label).sort()
).to.eql(['sayHello']);
});

it('gets completions for extended nodes with callfunc invocation - ensure overridden methods included', () => {
program.setFile('source/main.bs', `
function main()
Expand Down Expand Up @@ -1318,7 +1352,7 @@ describe('Program', () => {

expect(
(program.getCompletions(`${rootDir}/source/main.bs`, Position.create(2, 30))).map(x => x.label).sort()
).to.eql(['sayHello', 'sayHello2', 'sayHello2', 'sayHello3', 'sayHello4']);
).to.eql(['sayHello', 'sayHello2', 'sayHello3', 'sayHello4']);
});

describe('xml inheritance', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/bscPlugin/completions/CompletionsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ export class CompletionsProcessor {
}

public process() {
let completionsArray = [];
if (isBrsFile(this.event.file) && this.event.file.isPositionNextToTokenKind(this.event.position, TokenKind.Callfunc)) {
const xmlScopes = this.event.program.getScopes().filter((s) => isXmlScope(s)) as XmlScope[];
// is next to a @. callfunc invocation - must be an interface method.
//TODO refactor this to utilize the actual variable's component type (when available)
for (const scope of xmlScopes) {
let fileLinks = this.event.program.getStatementsForXmlFile(scope);
for (let fileLink of fileLinks) {
this.event.completions.push(scope.createCompletionFromFunctionStatement(fileLink.item));
let pushItem = scope.createCompletionFromFunctionStatement(fileLink.item);
if (!completionsArray.includes(pushItem.label)) {
completionsArray.push(pushItem.label);
this.event.completions.push(pushItem);
}
}
}
//no other result is possible in this case
Expand Down