Skip to content

Commit

Permalink
✨ Complete 'RANDOM' for UUIDs
Browse files Browse the repository at this point in the history
Resolve #440.
  • Loading branch information
SPGoding committed Jun 23, 2020
1 parent 46a68cc commit 935258c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
33 changes: 29 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fs-extra": "^9.0.0",
"python-range": "^1.0.4",
"synchronous-promise": "^2.0.12",
"uuid": "^8.1.0",
"vscode-json-languageservice": "^3.6.0",
"vscode-languageserver": "^6.1.1",
"vscode-languageserver-textdocument": "^1.0.1",
Expand All @@ -47,6 +48,7 @@
"@types/mocha": "^7.0.0",
"@types/node": "^14.0.0",
"@types/power-assert": "^1.5.2",
"@types/uuid": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^3.0.0",
"@typescript-eslint/parser": "^3.0.0",
"all-contributors-cli": "^6.14.2",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"code-snippets-invalid-for-game": "Code snippets are invalid for the game",
"collection-length.between": "a collection with length between %0% and %1%",
"collection-length.exact": "a collection with length %0%",
"completion.identity.this": "THIS",
"completion.uuid.random": "RANDOM",
"conjunction.and_2": " and ",
"conjunction.and_3+_1": ", ",
"conjunction.and_3+_2": ", and ",
Expand Down
8 changes: 4 additions & 4 deletions src/parsers/IdentityArgumentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class IdentityArgumentParser extends ArgumentParser<IdentityNode> {
//#endregion

//#region Completions: apply.
this.applyCompletions(complNamespaces, ans, ctx, complFolders, complFiles)
this.applyCompletions(ans, start, ctx, complNamespaces, complFolders, complFiles)
//#endregion

//#region Errors.
Expand Down Expand Up @@ -118,7 +118,7 @@ export class IdentityArgumentParser extends ArgumentParser<IdentityNode> {
))
}

private applyCompletions(complNamespaces: Set<string>, ans: ArgumentParserResult<IdentityNode>, ctx: ParsingContext, complFolders: Set<string>, complFiles: Set<string>) {
private applyCompletions(ans: ArgumentParserResult<IdentityNode>, start: number, ctx: ParsingContext, complNamespaces: Set<string>, complFolders: Set<string>, complFiles: Set<string>) {
// namespace -> CompletionItemKind.Module
// folder -> CompletionItemKind.Folder
// file -> CompletionItemKind.Field
Expand Down Expand Up @@ -151,9 +151,9 @@ export class IdentityArgumentParser extends ArgumentParser<IdentityNode> {
}))

// Add 'This' to completions
if (ctx.id) {
if (start === ctx.cursor && ctx.id) {
ans.completions.push({
label: 'THIS',
label: locale('completion.identity.this'),
insertText: ctx.id.toTagString(),
detail: ctx.id.toTagString(),
kind: CompletionItemKind.Snippet
Expand Down
14 changes: 14 additions & 0 deletions src/parsers/UuidArgumentParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { v4 as uuidV4 } from 'uuid'
import { CompletionItemKind } from 'vscode-languageserver'
import { locale } from '../locales'
import { ArgumentParserResult } from '../types/Parser'
import { ParsingContext } from '../types/ParsingContext'
Expand Down Expand Up @@ -34,6 +36,18 @@ export class UuidArgumentParser extends ArgumentParser<string> {
}
//#endregion

//#region Completions.
if (ctx.cursor === start) {
const randomUuid = uuidV4()
ans.completions.push({
label: locale('completion.uuid.random'),
insertText: randomUuid,
detail: randomUuid,
kind: CompletionItemKind.Snippet
})
}
//#endregion

ans.tokens.push(Token.from(start, reader, TokenType.number))

return ans
Expand Down
2 changes: 1 addition & 1 deletion src/test/parsers/IdentityArgumentParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('IdentityArgumentParser Tests', () => {
}
])
})
it('Should return completions for "This"', async () => {
it('Should return completions for "THIS"', async () => {
const ctx = constructContext({
registry: registries, parsers, cache, cursor: 0,
id: new IdentityNode('spgoding', ['this', 'is', 'the', 'current', 'function'])
Expand Down
10 changes: 10 additions & 0 deletions src/test/parsers/UuidArgumentParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ describe('UuidArgumentParser Tests', () => {
const actual = parser.parse(reader, ctx)
assert.deepStrictEqual(actual.errors, [])
assert.deepStrictEqual(actual.data, 'd9b38d97-d9e8-49a3-b225-3ffb4da40e2d')
assert.deepStrictEqual(actual.completions, [])
})
it('Should parse short UUID', () => {
const reader = new StringReader('40-0-27f-13-ee45000032e1')
const parser = new UuidArgumentParser()
const actual = parser.parse(reader, ctx)
assert.deepStrictEqual(actual.errors, [])
assert.deepStrictEqual(actual.data, '40-0-27f-13-ee45000032e1')
assert.deepStrictEqual(actual.completions, [])
})
it('Should return completions when the cursor is at the beginning', () => {
const reader = new StringReader('')
const parser = new UuidArgumentParser()
const actual = parser.parse(reader, constructContext({cursor: 0}))
assert(actual.completions.length === 1)
assert(actual.completions[0].label === 'RANDOM')
})
it('Should return errors', () => {
const reader = new StringReader('ASDASDASD')
Expand All @@ -34,6 +43,7 @@ describe('UuidArgumentParser Tests', () => {
'Expected a UUID but got ‘ASDASDASD’'
)])
assert.deepStrictEqual(actual.data, 'ASDASDASD')
assert.deepStrictEqual(actual.completions, [])
})
})
})

0 comments on commit 935258c

Please sign in to comment.