diff --git a/notes/highlight request.md b/notes/highlight request.md new file mode 100644 index 0000000..2ac2aac --- /dev/null +++ b/notes/highlight request.md @@ -0,0 +1,45 @@ +# Highlight Range Request + +request command: `mcfunction/highlightRange` + +request flow: Server -> Client + +Information 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..f27cfb2 --- /dev/null +++ b/src/misc_functions/highlight_util.ts @@ -0,0 +1,14 @@ +import { HighlightScope, SubAction } from "../types"; + +export function actionFromScopes(scopes: HighlightScope[]): SubAction[] { + return scopes.map(actionFromScope); +} + +export function actionFromScope(scope: HighlightScope): SubAction { + return { + data: scope.scopes, + 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/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(); } } diff --git a/src/test/parsers/literal.test.ts b/src/test/parsers/literal.test.ts index 6ff1cc0..290abfc 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,26 @@ 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: ["argument", "literal"], + high: 4, + low: 0, + type: "highlight" + } + ] + ); + }); }); describe("literal not matching", () => { it("should fail when the first character doesn't match", () => { diff --git a/src/types.ts b/src/types.ts index eccabb8..64c3135 100644 --- a/src/types.ts +++ b/src/types.ts @@ -110,10 +110,10 @@ export interface StoredParseResult { interface SubActionBase extends DataInterval { type: U; } - export type SubAction = - | SubActionBase<"hover", string> - | SubActionBase<"format", string>; + | SubActionBase<"format", string> + | SubActionBase<"highlight", string[]> + | SubActionBase<"hover", string>; // | SubActionBase<"rename", RenameRequest>; //#endregion export type Success = true; @@ -151,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; +}