Passing findToken tests

This commit is contained in:
Christopher Schleiden
2022-11-22 17:01:59 -08:00
parent 3eabb5ad81
commit 9f5ced7bd3
2 changed files with 18 additions and 29 deletions
@@ -67,9 +67,9 @@ describe("find-token", () => {
testFindToken(`on: testFindToken(`on:
pu|sh:`) pu|sh:`)
).toEqual({ ).toEqual({
parent: ["on-strict", TokenType.Mapping], parent: ["on-mapping-strict", TokenType.Mapping],
key: ["push-event-mapping", TokenType.String, "push"], key: null,
token: ["on-strict", TokenType.Mapping] token: [null, TokenType.String, "push"]
}); });
}); });
@@ -92,7 +92,7 @@ jo|bs:
).toEqual({ ).toEqual({
parent: ["workflow-root-strict", TokenType.Mapping], parent: ["workflow-root-strict", TokenType.Mapping],
key: null, key: null,
token: ["jobs", TokenType.Mapping] token: [null, TokenType.String, "jobs"]
}); });
}); });
}); });
+14 -25
View File
@@ -67,32 +67,21 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
for (let i = 0; i < mappingToken.count; i++) { for (let i = 0; i < mappingToken.count; i++) {
const {key, value} = mappingToken.get(i); const {key, value} = mappingToken.get(i);
if (onSameLine(pos, key, value)) { if (posInToken(pos, key)) {
if (posInToken(pos, key)) { return {
if (key.range!.end[1] + 1 === value.range!.start[1]) { token: key,
// There's no space between the key and value, this is not valid keyToken: null,
return { parent: mappingToken
token: null, };
keyToken: null, }
parent: null
};
}
return { // Empty nodes positions won't always match the cursor, so check if we're on the same line
token: key, if (emptyNode(value)) {
keyToken: null, return {
parent: mappingToken token: value,
}; keyToken: null,
} parent: key
};
// Empty nodes positions won't always match the cursor, so check if we're on the same line
if (emptyNode(value)) {
return {
token: value,
keyToken: null,
parent: key
};
}
} }
s.push({ s.push({