From 4b7f16422a05e28706a8515019d728afe654e695 Mon Sep 17 00:00:00 2001 From: MrYurihi Date: Sat, 23 Jun 2018 11:33:54 -0400 Subject: [PATCH 1/6] Add highlighting --- notes/highlight request.md | 55 ++++++++++++++++++++++++++++ notes/scope format.md | 48 ++++++++++++++++++++++++ src/misc_functions/highlight_util.ts | 20 ++++++++++ src/misc_functions/index.ts | 1 + src/types.ts | 5 ++- 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 notes/highlight request.md create mode 100644 notes/scope format.md create mode 100644 src/misc_functions/highlight_util.ts diff --git a/notes/highlight request.md b/notes/highlight request.md new file mode 100644 index 0000000..d6a05fb --- /dev/null +++ b/notes/highlight request.md @@ -0,0 +1,55 @@ +# Highlight Range Request + +request command: `mcfunction/highlightRange` + +request flow: Client -> Server -> Client + +Request data: + +```ts +{ + document: //Document: I forget what the TS type is + startLine: number, + endLine: number +} +``` + +Response data: + +```ts +{ + scopes: [ + { + line: number, + scopes: string[] + } + ]; +} +``` + +# Highlight Text Request + +request command: `mfunction/highlightText` + +request flow: Client -> Server -> Client + +Request data: + +```ts +{ + text: string[]; //Split lines +} +``` + +Response data: + +```ts +{ + scopes: [ + { + line: number, + scopes: string[] + } + ]; +} +``` diff --git a/notes/scope format.md b/notes/scope format.md new file mode 100644 index 0000000..df93bad --- /dev/null +++ b/notes/scope format.md @@ -0,0 +1,48 @@ +## Some notes + +Make sure that the begin and end are relative to the start of the line + +You _generally_ should not have multiple of the same scopes inside one another (directly) + +## Rules + +- Arguments should have the surrounding scope `["argument", "parser name"]`. + - Example: `summon ~ ~ ~ zombie` + +```ts +{ + end: 7, + scopes: ["argument", "minecraft:block_pos"], + start: 12 +} +``` + +- Scopes of multiple names should be separated by `-` + - Example: in `say @e[name="foo",tag=bar]` there should be + +```ts +{ + end: 18, + scopes: ["kvpair-separator", "separator"], + start: 17 +} +``` + +## Various scopes + +`"kvpair"`: A key-value pair, like `foo:bar` or `foo=bar` +`"*x*-separator"`: A separator between same scopes, like `"kvpair-separator"` would be `,` for NBT +`"*x*-*y*-separator"`: A separator between different scopes, like `"key-value-separator"` would be `:` for NBT +`"argument"`: A command argument. This shouldn't be used anywhere else +`"key"`: A key, like foo in `foo:bar` and `foo=bar` +`"value"`: A value, like bar in `foo:bar` and `foo=bar` +`"quote"`: A quote character, IE `"` +`"separator"`: Should accompany `*x*-separator` and `*x*-*y*-separator*` +`"*x*-start"`: Start of a value with characters as start and end markers (like `{` and `[`). An accompanying `"start"` should also exist +`"*x*-end"`: Same as `"*x*-start"` +`"punctuation"`: Should exist on all characters used as a separator or start/end +`"string"`: If the scoped value is a string +`"quoted"`: If the string is quoted +`"unquoted"`: If the string is unquoted +`"prefix"`: If the character is a prefix +`"suffix"`: Same as `"prefix"` diff --git a/src/misc_functions/highlight_util.ts b/src/misc_functions/highlight_util.ts new file mode 100644 index 0000000..1aea65d --- /dev/null +++ b/src/misc_functions/highlight_util.ts @@ -0,0 +1,20 @@ +import { SubAction } from "../types"; + +export interface HighlightScope { + end: number; + scopes: string[]; + start: number; +} + +export function actionFromScopes(scopes: HighlightScope[]): SubAction[] { + return scopes.map(actionFromScope); +} + +export function actionFromScope(scope: HighlightScope): SubAction { + return { + data: scope, + high: scope.end, + low: scope.start, + type: "highlight" + }; +} diff --git a/src/misc_functions/index.ts b/src/misc_functions/index.ts index 117da62..6b45907 100644 --- a/src/misc_functions/index.ts +++ b/src/misc_functions/index.ts @@ -8,3 +8,4 @@ export * from "./returnhelper"; export * from "./security"; export * from "./setup"; export * from "./translation"; +export * from "./highlight_util"; diff --git a/src/types.ts b/src/types.ts index eccabb8..791a14b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,6 +3,7 @@ import { CompletionItemKind } from "vscode-languageserver"; import { BlankCommandError, CommandError } from "./brigadier_components/errors"; import { StringReader } from "./brigadier_components/string_reader"; import { CommandNodePath, Datapack, GlobalData } from "./data/types"; +import { HighlightScope } from "./misc_functions"; //#region Document export interface FunctionInfo { @@ -110,10 +111,10 @@ export interface StoredParseResult { interface SubActionBase extends DataInterval { type: U; } - export type SubAction = | SubActionBase<"hover", string> - | SubActionBase<"format", string>; + | SubActionBase<"format", string> + | SubActionBase<"highlight", HighlightScope>; // | SubActionBase<"rename", RenameRequest>; //#endregion export type Success = true; From 407d597abff96b72c239889bf111aeafa1078bcd Mon Sep 17 00:00:00 2001 From: MrYurihi Date: Sat, 23 Jun 2018 11:36:46 -0400 Subject: [PATCH 2/6] Add highlight to literal --- src/parsers/literal.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/parsers/literal.ts b/src/parsers/literal.ts index 7dad508..9f098b5 100644 --- a/src/parsers/literal.ts +++ b/src/parsers/literal.ts @@ -1,5 +1,5 @@ import { CompletionItemKind } from "vscode-languageserver"; -import { ReturnHelper } from "../misc_functions"; +import { actionFromScope, ReturnHelper } from "../misc_functions"; import { Parser } from "../types"; const parser: Parser = { @@ -19,6 +19,13 @@ const parser: Parser = { if (reader.string.substring(begin, end) === literal) { reader.cursor = end; if (reader.peek() === " " || !reader.canRead()) { + helper.addActions( + actionFromScope({ + end: reader.cursor, + scopes: ["argument", "literal"], + start: begin + }) + ); return helper.succeed(); } } From 2c8012115167215e84692d3e29c1924d905015f8 Mon Sep 17 00:00:00 2001 From: MrYurihi Date: Sat, 23 Jun 2018 11:46:17 -0400 Subject: [PATCH 3/6] Add literal tests for highlighting --- src/test/parsers/literal.test.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/test/parsers/literal.test.ts b/src/test/parsers/literal.test.ts index 6ff1cc0..c326dc2 100644 --- a/src/test/parsers/literal.test.ts +++ b/src/test/parsers/literal.test.ts @@ -18,7 +18,8 @@ describe("literalArgumentParser", () => { defined(literalArgumentParser.parse(reader, properties)), true, [], - ["test"] + ["test"], + 1 ); assert.strictEqual(reader.cursor, 4); }); @@ -28,10 +29,30 @@ describe("literalArgumentParser", () => { defined(literalArgumentParser.parse(reader, properties)), true, [], - [] + [], + 1 ); assert.strictEqual(reader.cursor, 4); }); + it("should return the correct highlight data", () => { + const reader = new StringReader("test"); + const out = literalArgumentParser.parse(reader, properties); + assert.deepStrictEqual( + out.actions.filter(v => v.type === "highlight"), + [ + { + data: { + end: 4, + scopes: ["argument", "literal"], + start: 0 + }, + high: 4, + low: 0, + type: "highlight" + } + ] + ); + }); }); describe("literal not matching", () => { it("should fail when the first character doesn't match", () => { From b770377e7b825b5ce3ffc4d1475c81e847dfc3ff Mon Sep 17 00:00:00 2001 From: MrYurihi Date: Sat, 30 Jun 2018 12:53:18 -0400 Subject: [PATCH 4/6] fix reviewed stuff --- src/misc_functions/highlight_util.ts | 10 ++-------- src/types.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/misc_functions/highlight_util.ts b/src/misc_functions/highlight_util.ts index 1aea65d..f27cfb2 100644 --- a/src/misc_functions/highlight_util.ts +++ b/src/misc_functions/highlight_util.ts @@ -1,10 +1,4 @@ -import { SubAction } from "../types"; - -export interface HighlightScope { - end: number; - scopes: string[]; - start: number; -} +import { HighlightScope, SubAction } from "../types"; export function actionFromScopes(scopes: HighlightScope[]): SubAction[] { return scopes.map(actionFromScope); @@ -12,7 +6,7 @@ export function actionFromScopes(scopes: HighlightScope[]): SubAction[] { export function actionFromScope(scope: HighlightScope): SubAction { return { - data: scope, + data: scope.scopes, high: scope.end, low: scope.start, type: "highlight" diff --git a/src/types.ts b/src/types.ts index 791a14b..64c3135 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,6 @@ import { CompletionItemKind } from "vscode-languageserver"; import { BlankCommandError, CommandError } from "./brigadier_components/errors"; import { StringReader } from "./brigadier_components/string_reader"; import { CommandNodePath, Datapack, GlobalData } from "./data/types"; -import { HighlightScope } from "./misc_functions"; //#region Document export interface FunctionInfo { @@ -112,9 +111,9 @@ interface SubActionBase extends DataInterval { type: U; } export type SubAction = - | SubActionBase<"hover", string> | SubActionBase<"format", string> - | SubActionBase<"highlight", HighlightScope>; + | SubActionBase<"highlight", string[]> + | SubActionBase<"hover", string>; // | SubActionBase<"rename", RenameRequest>; //#endregion export type Success = true; @@ -152,3 +151,9 @@ export interface ReturnSuccess // Helper types to lower the amount of repetition of the names export type BCE = BlankCommandError; export type CE = CommandError; + +export interface HighlightScope { + end: number; + scopes: string[]; + start: number; +} From 8543360ec31fcab0d38c118756fb828aaece9484 Mon Sep 17 00:00:00 2001 From: MrYurihi Date: Sat, 30 Jun 2018 12:56:53 -0400 Subject: [PATCH 5/6] fix a test --- src/test/parsers/literal.test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/parsers/literal.test.ts b/src/test/parsers/literal.test.ts index c326dc2..290abfc 100644 --- a/src/test/parsers/literal.test.ts +++ b/src/test/parsers/literal.test.ts @@ -41,11 +41,7 @@ describe("literalArgumentParser", () => { out.actions.filter(v => v.type === "highlight"), [ { - data: { - end: 4, - scopes: ["argument", "literal"], - start: 0 - }, + data: ["argument", "literal"], high: 4, low: 0, type: "highlight" From 49c16902e3e1a28889c7bd9be00ef11691ea7fea Mon Sep 17 00:00:00 2001 From: MrYurihi Date: Sun, 1 Jul 2018 07:53:29 -0400 Subject: [PATCH 6/6] Update highlight request --- notes/highlight request.md | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/notes/highlight request.md b/notes/highlight request.md index d6a05fb..2ac2aac 100644 --- a/notes/highlight request.md +++ b/notes/highlight request.md @@ -2,19 +2,9 @@ request command: `mcfunction/highlightRange` -request flow: Client -> Server -> Client - -Request data: +request flow: Server -> Client -```ts -{ - document: //Document: I forget what the TS type is - startLine: number, - endLine: number -} -``` - -Response data: +Information data: ```ts {