Skip to content

Commit

Permalink
Fix corner cases in definition finding
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomi Turtiainen committed Mar 2, 2019
1 parent 585fc20 commit c2eeeff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
19 changes: 13 additions & 6 deletions server/src/intellisense/definition-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ export function findKeywordDefinition(
workspaceTree
);

return {
node: definition.keyword,
uri: definition.file.uri,
range: nodeLocationToRange(definition.keyword),
};
if (definition) {
return {
node: definition.keyword,
uri: definition.file.uri,
range: nodeLocationToRange(definition.keyword),
};
}
}

return null;
Expand Down Expand Up @@ -396,7 +398,12 @@ function tryFindKeywordDefinitionFromWorkspace(
function tryGetKeywordNameFromBddDefinition(
keywordName: Identifier
): Identifier | null {
const [, firstWord, rest] = /([^ ]+)(?: )(.*)/.exec(keywordName.name);
const matches = /([^ ]+)(?: )(.*)/.exec(keywordName.name);
if (!matches) {
return null;
}

const [, firstWord, rest] = matches;
if (gherkingIdentifiers.has(firstWord.toLowerCase())) {
// Let's just use the same location even tho it's not 100% accurate
return new Identifier(rest, keywordName.location);
Expand Down
4 changes: 4 additions & 0 deletions server/src/intellisense/test/data/definition-finder.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ Test cases for findinging gherking style keyword definition
Then Simple Keyword
and Simple Keyword
but Simple Keyword
Test cases for not existing keywords
Non Existing Keyword
Given Non Existing Gherkin Keyword
`,
};
30 changes: 30 additions & 0 deletions server/src/intellisense/test/definition-finder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ describe("Definition finder", () => {
runTest(15, 2);
});
});

it("non existing keyword", () => {
const actual = findDefinition(
{
filePath: "tests.robot",
position: {
line: 18,
column: 2,
},
},
workspace
);

assert.isNull(actual);
});

it("non existing gherkin keyword", () => {
const actual = findDefinition(
{
filePath: "tests.robot",
position: {
line: 19,
column: 2,
},
},
workspace
);

assert.isNull(actual);
});
});
});
});

0 comments on commit c2eeeff

Please sign in to comment.