-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow plugins to contribute completions (#647)
* Initial commit. * Fix test * Update src/Program.spec.ts Co-authored-by: christopher Dwyer-Perkins <[email protected]> * update plugin docs Co-authored-by: christopher Dwyer-Perkins <[email protected]>
- Loading branch information
1 parent
0b3d31a
commit 5cc5ea3
Showing
7 changed files
with
143 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { isBrsFile, isXmlScope } from '../../astUtils/reflection'; | ||
import type { ProvideCompletionsEvent } from '../../interfaces'; | ||
import { TokenKind } from '../../lexer/TokenKind'; | ||
import type { XmlScope } from '../../XmlScope'; | ||
import { util } from '../../util'; | ||
|
||
export class CompletionsProcessor { | ||
constructor( | ||
private event: ProvideCompletionsEvent | ||
) { | ||
|
||
} | ||
|
||
public process() { | ||
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)); | ||
} | ||
} | ||
//no other result is possible in this case | ||
return; | ||
} | ||
|
||
//find the scopes for this file | ||
let scopesForFile = this.event.program.getScopesForFile(this.event.file); | ||
|
||
//if there are no scopes, include the global scope so we at least get the built-in functions | ||
scopesForFile = scopesForFile.length > 0 ? scopesForFile : [this.event.program.globalScope]; | ||
|
||
//get the completions from all scopes for this file | ||
let allCompletions = util.flatMap( | ||
scopesForFile.map(scope => { | ||
return this.event.file.getCompletions(this.event.position, scope); | ||
}), | ||
c => c | ||
); | ||
|
||
//only keep completions common to every scope for this file | ||
let keyCounts = new Map<string, number>(); | ||
for (let completion of allCompletions) { | ||
let key = `${completion.label}-${completion.kind}`; | ||
keyCounts.set(key, keyCounts.has(key) ? keyCounts.get(key) + 1 : 1); | ||
if (keyCounts.get(key) === scopesForFile.length) { | ||
this.event.completions.push(completion); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters