Skip to content

Commit

Permalink
Prevent completion error for first token in file
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Feb 26, 2021
1 parent 2fde0f9 commit 919f558
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ describe('BrsFile', () => {
expect(names).to.contain('m');
});

it('includes all keywordsm`', () => {
it('does not fail for missing previousToken', () => {
//add a single character to the file, and get completions after it
program.addOrReplaceFile('source/main.brs', `i`);
expect(() => {
program.getCompletions(`${rootDir}/source/main.brs`, Position.create(0, 1)).map(x => x.label);
}).not.to.throw;
});

it('includes all keywords`', () => {
//eslint-disable-next-line @typescript-eslint/no-floating-promises
program.addOrReplaceFile({ src: `${rootDir}/source/main.brs`, dest: 'source/main.brs' }, `
sub Main()
Expand Down
9 changes: 5 additions & 4 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,17 +1073,18 @@ export class BrsFile {
}

public isPositionNextToTokenKind(position: Position, tokenKind: TokenKind) {
let closestToken = this.getClosestToken(position);
let previousToken = this.getPreviousToken(closestToken);
const closestToken = this.getClosestToken(position);
const previousToken = this.getPreviousToken(closestToken);
const previousTokenKind = previousToken?.kind;
//next to matched token
if (!closestToken || closestToken.kind === TokenKind.Eof) {
return false;
} else if (closestToken.kind === tokenKind) {
return true;
} else if (closestToken.kind === TokenKind.Newline || previousToken.kind === TokenKind.Newline) {
} else if (closestToken.kind === TokenKind.Newline || previousTokenKind === TokenKind.Newline) {
return false;
//next to an identifier, which is next to token kind
} else if (closestToken.kind === TokenKind.Identifier && previousToken.kind === tokenKind) {
} else if (closestToken.kind === TokenKind.Identifier && previousTokenKind === tokenKind) {
return true;
} else {
return false;
Expand Down

0 comments on commit 919f558

Please sign in to comment.