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

Fix completions in non-main ts and py files #6699

Merged
merged 2 commits into from
Mar 12, 2020
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
17 changes: 13 additions & 4 deletions pxtcompiler/emitter/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ namespace ts.pxtc.service {
host.setFile(fileName, fileContent);
}

const tsFilename = filenameWithExtension(fileName, "ts");

const span: PosSpan = { startPos: wordStartPos, endPos: wordEndPos }

const isPython = /\.py$/.test(fileName);
Expand All @@ -884,7 +886,6 @@ namespace ts.pxtc.service {

let lastNl = src.lastIndexOf("\n", position)
lastNl = Math.max(0, lastNl)
const cursorLine = src.substring(lastNl, position)

if (dotIdx != -1)
complPosition = dotIdx
Expand Down Expand Up @@ -919,15 +920,15 @@ namespace ts.pxtc.service {
// update our language host
Object.keys(res.outfiles)
.forEach(k => {
if (k.endsWith(".ts")) {
if (k === tsFilename) {
host.setFile(k, res.outfiles[k])
}
})

// convert our location from python to typescript
if (res.sourceMap) {
const pySrc = src
const tsSrc = res.outfiles["main.ts"]
const tsSrc = res.outfiles[tsFilename]
const srcMap = pxtc.BuildSourceMapHelpers(res.sourceMap, tsSrc, pySrc)

const smallest = srcMap.py.smallestOverlap(span)
Expand All @@ -940,7 +941,7 @@ namespace ts.pxtc.service {
}

const prog = service.getProgram()
const tsAst = prog.getSourceFile("main.ts") // TODO: work for non-main files
const tsAst = prog.getSourceFile(tsFilename)
const tc = prog.getTypeChecker()
let isPropertyAccess = false;

Expand Down Expand Up @@ -1873,6 +1874,9 @@ namespace ts.pxtc.service {

function findInnerMostNodeAtPosition(n: Node, position: number): Node | null {
for (let child of n.getChildren()) {
if (child.kind >= ts.SyntaxKind.FirstPunctuation && child.kind <= ts.SyntaxKind.LastPunctuation)
continue;

let s = child.getStart()
let e = child.getEnd()
if (s <= position && position < e)
Expand All @@ -1894,4 +1898,9 @@ namespace ts.pxtc.service {

return tc.typeToString(t);
}

function filenameWithExtension(filename: string, extension: string) {
if (extension.charAt(0) === ".") extension = extension.substr(1);
return filename.substr(0, filename.lastIndexOf(".") + 1) + extension;
}
}
3 changes: 3 additions & 0 deletions tests/language-service/cases/completions_ignore_tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(testNamespace). // testNamespace.someFunction

;["hello"]. // Array.length; Array.push; Array.set
3 changes: 0 additions & 3 deletions tests/language-service/languageservicerunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe("language service", () => {

for (const testCase of cases) {
it("get completions " + testCase.fileName + testCase.position, () => {
console.log(JSON.stringify(testCase));
return runCompletionTestCaseAsync(testCase);
});
}
Expand Down Expand Up @@ -108,8 +107,6 @@ function runCompletionTestCaseAsync(testCase: CompletionTestCase) {
testCase.fileText
);

console.log(result.entries.map(e => e.qName).join(", "))

for (const sym of testCase.expectedSymbols) {
chai.assert(result.entries.some(s => (testCase.isPython ? s.pyQName : s.qName) === sym), `Did not receive symbol '${sym}'`);
}
Expand Down