forked from ember-tooling/ember-language-server
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic template imports support (<template> tag and gts, gjs fil…
…es) (#350) * draft * file renages test * range walker test * add bounds tests * tests for expected corner cases * smoke template locals test * improve subtract logic * cleanup * scope strip example * use char placeholder * scope locator * implement placeholders * extract scope bindings * support inline scope extraction * basic completion provider based on js scope * basic unit test for completion provider * get test working * absolute content part support * query legacy template completion logic for results * fix snapshot * fix linting error * support {gts,gjs} in tests, components * use emoji for pointer * get basic autocomplete working * support non-instrumented runs * add gts/gjs integration tests * add template lint support * add gts and gjs to list of supported extensions * fix absolute content boundaries * hbs source convert * attempt to get autofixes working
- Loading branch information
Showing
19 changed files
with
1,309 additions
and
169 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
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
95 changes: 95 additions & 0 deletions
95
src/completion-provider/glimmer-script-completion-provider.ts
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,95 @@ | ||
import { CompletionItem, TextDocumentPositionParams } from 'vscode-languageserver/node'; | ||
import Server from '../server'; | ||
import { getFileRanges, RangeWalker, getPlaceholderPathFromAst, getScope } from '../utils/glimmer-script'; | ||
import { parseScriptFile as parse } from 'ember-meta-explorer'; | ||
import { containsPosition, toPosition } from '../estree-utils'; | ||
import { getFocusPath } from '../utils/glimmer-template'; | ||
import { TextDocument } from 'vscode-languageserver-textdocument'; | ||
|
||
export default class GlimmerScriptCompletionProvider { | ||
constructor(private server: Server) {} | ||
async provideCompletions(params: TextDocumentPositionParams): Promise<CompletionItem[]> { | ||
const document = this.server.documents.get(params.textDocument.uri); | ||
|
||
if (!document) { | ||
return []; | ||
} | ||
|
||
const rawContent = document.getText(); | ||
|
||
const ranges = getFileRanges(rawContent); | ||
|
||
let rangeWalker = new RangeWalker(ranges); | ||
|
||
// strip not needed scopes example | ||
rangeWalker = rangeWalker.subtract(rangeWalker.hbsInlineComments(true)); | ||
rangeWalker = rangeWalker.subtract(rangeWalker.hbsComments(true)); | ||
rangeWalker = rangeWalker.subtract(rangeWalker.htmlComments(true)); | ||
|
||
const templates = rangeWalker.templates(true); | ||
|
||
const cleanScriptWalker = rangeWalker.subtract(templates, true); | ||
|
||
const templateForPosition = templates.find((el) => { | ||
return containsPosition( | ||
{ | ||
start: { | ||
line: el.loc.start.line, | ||
column: el.loc.start.character, | ||
}, | ||
end: { | ||
line: el.loc.end.line, | ||
column: el.loc.end.character, | ||
}, | ||
}, | ||
toPosition(params.position) | ||
); | ||
}); | ||
|
||
const ast = parse(cleanScriptWalker.content, { | ||
sourceType: 'module', | ||
}); | ||
|
||
if (templateForPosition) { | ||
const placeholder = getPlaceholderPathFromAst(ast, templateForPosition.key); | ||
|
||
if (!placeholder) { | ||
return []; | ||
} | ||
|
||
const results: CompletionItem[] = []; | ||
const scopes = getScope(placeholder.scope); | ||
|
||
scopes.forEach((name) => { | ||
results.push({ | ||
label: name, | ||
}); | ||
}); | ||
|
||
const synthDoc = TextDocument.create(document.uri, 'handlebars', document.version, templateForPosition.absoluteContent); | ||
const info = getFocusPath(synthDoc, params.position); | ||
|
||
if (!info) { | ||
return results; | ||
} | ||
|
||
const project = this.server.projectRoots.projectForUri(params.textDocument.uri); | ||
|
||
if (!project) { | ||
return results; | ||
} | ||
|
||
const legacyResults = await this.server.templateCompletionProvider.provideCompletionsForFocusPath(info, params.textDocument, params.position, project); | ||
|
||
legacyResults.forEach((result) => { | ||
results.push(result); | ||
}); | ||
|
||
return results; | ||
// do logic to get more meta from js scope for template position | ||
// here we need glimmer logic to collect all available tokens from scope for autocomplete | ||
} else { | ||
return []; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.