From 4708a6167df15022a0a8e2d4ccb5db240af8c480 Mon Sep 17 00:00:00 2001 From: seven Date: Wed, 3 Jul 2024 01:43:05 +0800 Subject: [PATCH 1/7] init branch [wip] Signed-off-by: seven --- src/common/searchLang.ts | 294 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 src/common/searchLang.ts diff --git a/src/common/searchLang.ts b/src/common/searchLang.ts new file mode 100644 index 0000000..27ca9ba --- /dev/null +++ b/src/common/searchLang.ts @@ -0,0 +1,294 @@ +import { executeActions } from './editor'; + +export interface LangModuleType { + id: string; + lexerRules?: unknown; + languageConfiguration?: unknown; + getSuggestionProvider?: unknown; +} + +export const searchLang: LangModuleType = { + id: 'search', + lexerRules: { + defaultToken: 'invalid', + tokenPostfix: '.search', + + // keywords of elasticsearch + keywords: [ + 'GET', + 'POST', + 'PUT', + 'DELETE', + 'HEAD', + 'OPTIONS', + 'PATCH', + 'TRACE', + 'index', + 'indices', + 'type', + 'types', + 'from', + 'size', + 'explain', + 'analyze', + 'default_operator', + 'df', + 'analyzer', + 'lenient', + 'lowercase_expanded_terms', + 'analyze_wildcard', + 'all_shards', + 'allow_no_indices', + 'expand_wildcards', + 'preference', + 'routing', + 'ignore_unavailable', + 'allow_no_indices', + 'ignore_throttled', + 'search_type', + 'batched_reduce_size', + 'ccs_minimize_roundtrips', + 'max_concurrent_shard_requests', + 'pre_filter_shard_size', + 'rest_total_hits_as_int', + 'scroll', + 'search_type', + 'typed_keys', + 'wait_for_active_shards', + 'wait_for_completion', + 'requests_per_second', + 'slices', + 'timeout', + 'terminate_after', + 'stats', + 'version', + 'version_type', + 'if_seq_no', + 'if_primary_term', + 'refresh', + 'routing', + 'parent', + 'preference', + 'realtime', + 'refresh', + 'retry_on_conflict', + 'timeout', + 'version', + 'version_type', + 'if_seq_no', + 'if_primary_term', + 'pipeline', + 'wait_for_active_shards', + 'wait_for_completion', + 'requests_per_second', + 'slices', + 'timeout', + 'terminate_after', + 'stats', + 'version', + 'version_type', + 'if_seq_no', + 'if_primary_term', + 'refresh', + 'routing', + 'parent', + 'preference', + 'realtime', + 'refresh', + 'retry_on_conflict', + 'timeout', + 'version', + 'version_type', + 'if_seq_no', + 'if_primary_term', + 'pipeline', + 'wait_for_active_shards', + 'wait_for_completion', + 'requests_per_second', + 'slices', + 'timeout', + 'terminate_after', + 'stats', + 'version', + 'version_type', + '_search', + ], + + typeKeywords: ['any', 'boolean', 'number', 'object', 'string', 'undefined'], + + // we include these common regular expressions + symbols: /[=> Date: Fri, 5 Jul 2024 02:59:44 +0800 Subject: [PATCH 2/7] feat: dependencies update Signed-off-by: seven --- src-tauri/Cargo.lock | 65 ++++++++++---------------------------------- src-tauri/Cargo.toml | 2 +- 2 files changed, 15 insertions(+), 52 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index fe7f44b..fd8815d 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -132,12 +132,6 @@ dependencies = [ "system-deps 6.2.2", ] -[[package]] -name = "atomic-waker" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" - [[package]] name = "autocfg" version = "1.3.0" @@ -1302,25 +1296,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "h2" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" -dependencies = [ - "atomic-waker", - "bytes", - "fnv", - "futures-core", - "futures-sink", - "http 1.1.0", - "indexmap 2.2.6", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -1464,7 +1439,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.3.26", + "h2", "http 0.2.12", "http-body 0.4.6", "httparse", @@ -1487,7 +1462,6 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.5", "http 1.1.0", "http-body 1.0.0", "httparse", @@ -1514,6 +1488,7 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", + "webpki-roots", ] [[package]] @@ -1529,22 +1504,6 @@ dependencies = [ "tokio-native-tls", ] -[[package]] -name = "hyper-tls" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" -dependencies = [ - "bytes", - "http-body-util", - "hyper 1.4.0", - "hyper-util", - "native-tls", - "tokio", - "tokio-native-tls", - "tower-service", -] - [[package]] name = "hyper-util" version = "0.1.6" @@ -2780,11 +2739,11 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.3.26", + "h2", "http 0.2.12", "http-body 0.4.6", "hyper 0.14.29", - "hyper-tls 0.5.0", + "hyper-tls", "ipnet", "js-sys", "log", @@ -2819,23 +2778,19 @@ checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37" dependencies = [ "base64 0.22.1", "bytes", - "encoding_rs", "futures-core", "futures-util", - "h2 0.4.5", "http 1.1.0", "http-body 1.0.0", "http-body-util", "hyper 1.4.0", "hyper-rustls", - "hyper-tls 0.6.0", "hyper-util", "ipnet", "js-sys", "log", "mime", "mime_guess", - "native-tls", "once_cell", "percent-encoding", "pin-project-lite", @@ -2848,9 +2803,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "sync_wrapper 1.0.1", - "system-configuration", "tokio", - "tokio-native-tls", "tokio-rustls", "tokio-util", "tower-service", @@ -2859,6 +2812,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", + "webpki-roots", "winreg 0.52.0", ] @@ -4433,6 +4387,15 @@ dependencies = [ "system-deps 6.2.2", ] +[[package]] +name = "webpki-roots" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd7c23921eeb1713a4e851530e9b9756e4fb0e89978582942612524cf09f01cd" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "webview2-com" version = "0.19.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e061893..8b1598c 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,7 +17,7 @@ serde_json = "1" tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } tauri-plugin-system-info = { git = "https://github.com/HuakunShen/tauri-plugin-system-info", branch = "v1" } # use v2 branch for Tauri v2 plugin async-openai = { version = "0.23.3" } -reqwest = { version = "0.12", features = ["json"] } +reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] } tokio = { version = "1", features = ["full"] } http = "0.2.12" log = "0.4.22" From cdcf41ff35c776d891e7098b6fc1f2f490225d6f Mon Sep 17 00:00:00 2001 From: seven Date: Sat, 6 Jul 2024 16:21:33 +0800 Subject: [PATCH 3/7] refactor: extract monaco configs Signed-off-by: seven --- src/common/monaco/environment.ts | 39 ++++ src/common/monaco/index.ts | 27 +++ .../{searchLang.ts => monaco/lexerRules.ts} | 219 +++++++++--------- src/views/editor/index.vue | 59 +---- 4 files changed, 175 insertions(+), 169 deletions(-) create mode 100644 src/common/monaco/environment.ts create mode 100644 src/common/monaco/index.ts rename src/common/{searchLang.ts => monaco/lexerRules.ts} (55%) diff --git a/src/common/monaco/environment.ts b/src/common/monaco/environment.ts new file mode 100644 index 0000000..3f1ce73 --- /dev/null +++ b/src/common/monaco/environment.ts @@ -0,0 +1,39 @@ +/** + * refer https://github.com/wobsoriano/codeplayground + * https://github.com/wobsoriano/codeplayground/blob/master/src/components/MonacoEditor.vue + */ +export const monacoEnvironment = { + // @ts-ignore + async getWorker(_, label) { + let worker; + + switch (label) { + case 'json': + // @ts-ignore + worker = await import('monaco-editor/esm/vs/language/json/json.worker?worker'); + break; + case 'css': + case 'scss': + case 'less': + // @ts-ignore + worker = await import('monaco-editor/esm/vs/language/css/css.worker?worker'); + break; + case 'html': + case 'handlebars': + case 'razor': + // @ts-ignore + worker = await import('monaco-editor/esm/vs/language/html/html.worker?worker'); + break; + case 'typescript': + case 'javascript': + // @ts-ignore + worker = await import('monaco-editor/esm/vs/language/typescript/ts.worker?worker'); + break; + default: + // @ts-ignore + worker = await import('monaco-editor/esm/vs/editor/editor.worker?worker'); + } + + return new worker.default(); + }, +}; diff --git a/src/common/monaco/index.ts b/src/common/monaco/index.ts new file mode 100644 index 0000000..8f6bf44 --- /dev/null +++ b/src/common/monaco/index.ts @@ -0,0 +1,27 @@ +// import 'monaco-editor/esm/vs/base/common/worker/simpleWorker'; +// import 'monaco-editor/esm/vs/base/worker/defaultWorkerFactory'; +// +// import 'monaco-editor/esm/vs/editor/browser/controller/coreCommands.js'; +// import 'monaco-editor/esm/vs/editor/browser/widget/codeEditorWidget.js'; +// +// import 'monaco-editor/esm/vs/editor/contrib/wordOperations/wordOperations.js'; +// +// import 'monaco-editor/esm/vs/editor/contrib/suggest/suggestController.js'; +// import 'monaco-editor/esm/vs/editor/contrib/hover/hover.js'; +// import 'monaco-editor/esm/vs/editor/contrib/parameterHints/parameterHints.js'; +// import 'monaco-editor/esm/vs/language/json/json.worker.js'; +/* eslint-disable-next-line @osd/eslint/module_migration */ +import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; + +import { search } from './lexerRules.ts'; +import { monacoEnvironment } from './environment.ts'; + +self.MonacoEnvironment = monacoEnvironment; + +monaco.languages.register({ id: search.id }); +monaco.languages.setMonarchTokensProvider( + search.id, + search.rules as monaco.languages.IMonarchLanguage, +); + +export { monaco }; diff --git a/src/common/searchLang.ts b/src/common/monaco/lexerRules.ts similarity index 55% rename from src/common/searchLang.ts rename to src/common/monaco/lexerRules.ts index 27ca9ba..e68ba39 100644 --- a/src/common/searchLang.ts +++ b/src/common/monaco/lexerRules.ts @@ -1,15 +1,110 @@ -import { executeActions } from './editor'; +const xJson = { + id: 'xjson', + rules: { + defaultToken: 'invalid', + tokenPostfix: '', + escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, + tokenizer: { + root: [ + [ + /("(?:[^"]*_)?script"|"inline"|"source")(\s*?)(:)(\s*?)(""")/, + [ + 'variable', + 'whitespace', + 'ace.punctuation.colon', + 'whitespace', + { + token: 'punctuation.start_triple_quote', + nextEmbedded: 'painless', + next: 'my_painless', + }, + ], + ], + [ + /(:)(\s*?)(""")(sql)/, + [ + 'ace.punctuation.colon', + 'whitespace', + 'punctuation.start_triple_quote', + { + token: 'punctuation.start_triple_quote.lang_marker', + nextEmbedded: 'opensearchql', + next: 'my_sql', + }, + ], + ], + [/{/, { token: 'paren.lparen', next: '@push' }], + [/}/, { token: 'paren.rparen', next: '@pop' }], + [/[[(]/, { token: 'paren.lparen' }], + [/[\])]/, { token: 'paren.rparen' }], + [/,/, { token: 'punctuation.comma' }], + [/:/, { token: 'punctuation.colon' }], + [/\s+/, { token: 'whitespace' }], + [/["](?:(?:\\.)|(?:[^"\\]))*?["]\s*(?=:)/, { token: 'variable' }], + [/"""/, { token: 'string_literal', next: 'string_literal' }], + [/0[xX][0-9a-fA-F]+\b/, { token: 'constant.numeric' }], + [/[+-]?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/, { token: 'constant.numeric' }], + [/(?:true|false)\b/, { token: 'constant.language.boolean' }], + // strings + [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string + [ + /"/, + { + token: 'string.quote', + bracket: '@open', + next: '@string', + }, + ], + [/['](?:(?:\\.)|(?:[^'\\]))*?[']/, { token: 'invalid' }], + [/.+?/, { token: 'text' }], + [/\/\/.*$/, { token: 'invalid' }], + ], -export interface LangModuleType { - id: string; - lexerRules?: unknown; - languageConfiguration?: unknown; - getSuggestionProvider?: unknown; -} + my_painless: [ + [ + /"""/, + { + token: 'punctuation.end_triple_quote', + nextEmbedded: '@pop', + next: '@pop', + }, + ], + ], -export const searchLang: LangModuleType = { + my_sql: [ + [ + /"""/, + { + token: 'punctuation.end_triple_quote', + nextEmbedded: '@pop', + next: '@pop', + }, + ], + ], + + string: [ + [/[^\\"]+/, 'string'], + [/@escapes/, 'string.escape'], + [/\\./, 'string.escape.invalid'], + [/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }], + ], + + string_literal: [ + [/"""/, { token: 'punctuation.end_triple_quote', next: '@pop' }], + [/./, { token: 'multi_string' }], + ], + }, + }, +}; +export const executeActions = { + regexp: /^(GET|DELETE|POST|PUT)\s\w+/, + decorationClassName: 'action-execute-decoration', +}; + +export const search = { id: 'search', - lexerRules: { + rules: { + // Set defaultToken to invalid to see what you do not tokenize yet defaultToken: 'invalid', tokenPostfix: '.search', @@ -188,107 +283,9 @@ export const searchLang: LangModuleType = { ], }, }, - languageConfiguration: { - brackets: [ - ['{', '}'], - ['[', ']'], - ], - autoClosingPairs: [ - { open: '{', close: '}' }, - { open: '[', close: ']' }, - { open: '"', close: '"' }, - ], - }, }; -// @ts-ignore -const xjsonLexerRules = { - json_root: [ - { include: '@comments' }, - // { include: '@variables' }, - // { include: '@json' }, - { regex: /("\${\w+}")/, action: { token: 'variable.template' } }, - [ - /("(?:[^"]*_)?script"|"inline"|"source")(\s*?)(:)(\s*?)(""")/, - [ - 'variable', - 'whitespace', - 'ace.punctuation.colon', - 'whitespace', - { - token: 'punctuation.start_triple_quote', - nextEmbedded: 'painless', - next: 'my_painless', - }, - ], - ], - [ - /(:)(\s*?)(""")(sql)/, - [ - 'ace.punctuation.colon', - 'whitespace', - 'punctuation.start_triple_quote', - { - token: 'punctuation.start_triple_quote.lang_marker', - nextEmbedded: 'esql', - next: 'my_sql', - }, - ], - ], - [/{/, { token: 'paren.lparen', next: '@push' }], - [/}/, { token: 'paren.rparen', next: '@pop' }], - [/[[(]/, { token: 'paren.lparen' }], - [/[\])]/, { token: 'paren.rparen' }], - [/,/, { token: 'punctuation.comma' }], - [/:/, { token: 'punctuation.colon' }], - [/\s+/, { token: 'whitespace' }], - [/["](?:(?:\\.)|(?:[^"\\]))*?["]\s*(?=:)/, { token: 'variable' }], - [/"""/, { token: 'string_literal', next: 'string_literal' }], - [/0[xX][0-9a-fA-F]+\b/, { token: 'constant.numeric' }], - [/[+-]?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/, { token: 'constant.numeric' }], - [/(?:true|false)\b/, { token: 'constant.language.boolean' }], - // strings - [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-terminated string - [ - /"/, - { - token: 'string.quote', - bracket: '@open', - next: '@string', - }, - ], - [/['](?:(?:\\.)|(?:[^'\\]))*?[']/, { token: 'invalid' }], - [/.+?/, { token: 'text' }], - [/\/\/.*$/, { token: 'invalid' }], - ], - my_painless: [ - [ - /"""/, - { - token: 'punctuation.end_triple_quote', - nextEmbedded: '@pop', - next: '@pop', - }, - ], - ], - my_sql: [ - [ - /"""/, - { - token: 'punctuation.end_triple_quote', - nextEmbedded: '@pop', - next: '@pop', - }, - ], - ], - string: [ - [/[^\\"]+/, 'string'], - [/@escapes/, 'string.escape'], - [/\\./, 'string.escape.invalid'], - [/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }], - ], - string_literal: [ - [/"""/, { token: 'punctuation.end_triple_quote', next: '@pop' }], - [/\\""""/, { token: 'punctuation.end_triple_quote', next: '@pop' }], - [/./, { token: 'multi_string' }], - ], + +export const registerLexerRules = (m: un) => { + // m.languages.register({ id: xJson.id }); + // m.languages.setMonarchTokensProvider(xJson.id, xJson.rules); }; diff --git a/src/views/editor/index.vue b/src/views/editor/index.vue index 2f626ca..f84230b 100644 --- a/src/views/editor/index.vue +++ b/src/views/editor/index.vue @@ -11,7 +11,6 @@