Skip to content

Commit

Permalink
fix: clean editor language rely (goplus#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang authored Mar 13, 2024
1 parent 1a43606 commit 91f0f76
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 260 deletions.
148 changes: 148 additions & 0 deletions spx-gui/src/components/code-editor/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import function_completions from './snippet'
import { keywords, brackets, typeKeywords, operators } from './language'
import { monaco } from '.'
export const editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
language: 'spx', // define the language mode
minimap: { enabled: true },
selectOnLineNumbers: true, // select the line number's of the code
roundedSelection: true, // rounded selection
readOnly: false, // read/write
cursorStyle: 'line', // line, block, 'line-thin', 'block-outline', 'underline', 'underline-thin'
automaticLayout: true, // auto layout
glyphMargin: true, // the margin is used for glyph margin and line numbers
useTabStops: false, // use tab key
renderControlCharacters: false, // render control characters
fontSize: 16, // font size
quickSuggestionsDelay: 100, // quick suggestions
wordWrapColumn: 40,
tabSize: 4, // tab size
folding: true, // code folding
foldingHighlight: true, // 折叠等高线
foldingStrategy: 'indentation', // 折叠方式 auto | indentation
showFoldingControls: 'mouseover', // 是否一直显示折叠 always | mouseover
disableLayerHinting: true // 等宽优
}
export const MonarchTokensProviderConfig:
| monaco.languages.IMonarchLanguage
| monaco.Thenable<monaco.languages.IMonarchLanguage> = {
defaultToken: 'invalid',
keywords,
typeKeywords,
operators,
functions: function_completions.map((e) => e.label),
brackets,
symbols: /[=><!~?:&|+\-*/^%]+/,
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
// digits
digits: /\d+(_+\d+)*/,
octaldigits: /[0-7]+(_+[0-7]+)*/,
binarydigits: /[0-1]+(_+[0-1]+)*/,
hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
// The main tokenizer for our languages
tokenizer: {
root: [
[
/[a-z_$][\w$]*/,
{
cases: {
'@typeKeywords': 'typeKeywords',
'@keywords': 'keyword',
'@functions': 'functions',
'@default': 'identifier'
}
}
],
[/[A-Z][\w$]*/, 'type.identifier'],

// whitespace
{ include: '@whitespace' },

// delimiters and operators
[/[{}()[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[
/@symbols/,
{
cases: {
'@operators': 'operator',
'@default': ''
}
}
],

// numbers
[/(@digits)[eE]([-+]?(@digits))?/, 'number.float'],
[/(@digits)\.(@digits)([eE][-+]?(@digits))?/, 'number.float'],
[/0[xX](@hexdigits)/, 'number.hex'],
[/0[oO]?(@octaldigits)/, 'number.octal'],
[/0[bB](@binarydigits)/, 'number.binary'],
[/(@digits)/, 'number'],

// delimiter: after number because of .\d floats
[/[;,.]/, 'delimiter'],
[/[=><!~?:&|+\-*/^%]+/, 'operator'],

[/"([^"\\]|\\.)*$/, 'string.invalid'],
[/"/, { token: 'string.quote', bracket: '@open', next: '@string' }],

// characters
[/'[^\\']'/, 'string'],
[/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
[/'/, 'string.invalid']
],
comment: [
[/[^/*]+/, 'comment'],
[/\/\*/, 'comment', '@push'],
['\\*/', 'comment', '@pop'],
[/[/*]/, 'comment']
],

string: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }]
],

whitespace: [
[/[ \t\r\n]+/, 'white'],
[/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment']
],

bracketCounting: [
[/{/, 'delimiter.bracket', '@bracketCounting'],
[/}/, 'delimiter.bracket', '@pop']
]
}
}
export const LanguageConfig: monaco.languages.LanguageConfiguration = {
// tokenize all words as identifiers
wordPattern: /(-?\d*\.\d\w*)|([^`~!@#%^&*()\-=+[{\]}\\|;:'",.<>/?\s]+)/g,
comments: {
lineComment: '//',
blockComment: ['/*', '*/']
},
brackets: [
['{', '}'],
['[', ']'],
['(', ')'],
['"', '"'],
["'", "'"]
],
onEnterRules: [
// if current cursor is betwwen work and space,then will not indent
{
beforeText: /\w/,
afterText: /^$/,
action: { indentAction: monaco.languages.IndentAction.None }
},
{
// match /*
beforeText: /^\s*(\/\*)/,
// match */
afterText: /^\s*\*\/$/,
action: { indentAction: monaco.languages.IndentAction.IndentOutdent }
}
]
}
58 changes: 26 additions & 32 deletions spx-gui/src/components/code-editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,44 @@
* @LastEditors: Zhang Zhi Yang
* @LastEditTime: 2024-02-04 13:15:49
* @FilePath: /spx-gui/src/components/code-editor/index.ts
* @Description:
* @Description:
*/
import * as monaco from 'monaco-editor'
import CodeEditor from "./CodeEditor.vue"
import CodeEditor from './CodeEditor.vue'

import { register } from './register';
import { editorOptions } from "./language"
export default CodeEditor;
import { register } from './register'
import { editorOptions } from './editor'
export default CodeEditor

export interface Snippet extends monaco.languages.CompletionItem {
}
export interface Snippet extends monaco.languages.CompletionItem {}

export interface FormatError {
Column: number;
Line: number;
Msg: string;
};
Column: number
Line: number
Msg: string
}
export interface FormatResponse {
Body: string;
Error: FormatError;
};

Body: string
Error: FormatError
}

export interface EditorOptions {
minimap?: {
enabled: boolean
}
readOnly?: boolean
cursorStyle?: "line" // line, block, 'line-thin', 'block-outline', 'underline', 'underline-thin'
minimap?: {
enabled: boolean
}
readOnly?: boolean
cursorStyle?: 'line' // line, block, 'line-thin', 'block-outline', 'underline', 'underline-thin'
}

export interface CodeEditorProps {
modelValue: string
height?: string
width?: string
editorOptions?: EditorOptions
modelValue: string
height?: string
width?: string
editorOptions?: EditorOptions
}
export interface CodeEditorEmits {
(e: 'change', value: string): void
(e: 'update:modelValue', value: string): void
}
export * from "./snippet"
export {
monaco,
register,
editorOptions,
(e: 'change', value: string): void
(e: 'update:modelValue', value: string): void
}
export * from './snippet'
export { monaco, register, editorOptions }
150 changes: 0 additions & 150 deletions spx-gui/src/components/code-editor/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
* @FilePath: /builder/spx-gui/src/components/code-editor/Language.ts
* @Description:
*/
import { monaco } from "."
import function_completions from "./snippet";

const keywords = [
"func",
"main",
Expand Down Expand Up @@ -95,158 +92,11 @@ const operators = [
">>=",
"=>",
];

const brackets = [
{ open: "{", close: "}", token: "delimiter.curly" },
{ open: "[", close: "]", token: "delimiter.bracket" },
{ open: "(", close: ")", token: "delimiter.parenthesis" },
];

// editor options
export const editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
language: "spx", // define the language mode
minimap: { enabled: true },
selectOnLineNumbers: true, // select the line number's of the code
roundedSelection: true, // rounded selection
readOnly: false, // read/write
cursorStyle: "line", // line, block, 'line-thin', 'block-outline', 'underline', 'underline-thin'
automaticLayout: true, // auto layout
glyphMargin: true, // the margin is used for glyph margin and line numbers
useTabStops: false, // use tab key
renderControlCharacters: false, // render control characters
fontSize: 16, // font size
quickSuggestionsDelay: 100, // quick suggestions
wordWrapColumn: 40,
tabSize: 4, // tab size
folding: true, // code folding
foldingHighlight: true, // 折叠等高线
foldingStrategy: "indentation", // 折叠方式 auto | indentation
showFoldingControls: "mouseover", // 是否一直显示折叠 always | mouseover
disableLayerHinting: true, // 等宽优
};
export const MonarchTokensProviderConfig:
| monaco.languages.IMonarchLanguage
| monaco.Thenable<monaco.languages.IMonarchLanguage> = {
defaultToken: "invalid",
keywords,
typeKeywords,
operators,
functions: function_completions.map((e) => e.label),
brackets,
symbols: /[=><!~?:&|+\-*/^%]+/,
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
// digits
digits: /\d+(_+\d+)*/,
octaldigits: /[0-7]+(_+[0-7]+)*/,
binarydigits: /[0-1]+(_+[0-1]+)*/,
hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
// The main tokenizer for our languages
tokenizer: {
root: [
[
/[a-z_$][\w$]*/,
{
cases: {
"@typeKeywords": "typeKeywords",
"@keywords": "keyword",
"@functions": "functions",
"@default": "identifier",
},
},
],
[/[A-Z][\w$]*/, "type.identifier"],

// whitespace
{ include: '@whitespace' },

// delimiters and operators
[/[{}()[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[/@symbols/, {
cases: {
'@operators': 'operator',
'@default': ''
}
}],

// numbers
[/(@digits)[eE]([-+]?(@digits))?/, 'number.float'],
[/(@digits)\.(@digits)([eE][-+]?(@digits))?/, 'number.float'],
[/0[xX](@hexdigits)/, 'number.hex'],
[/0[oO]?(@octaldigits)/, 'number.octal'],
[/0[bB](@binarydigits)/, 'number.binary'],
[/(@digits)/, 'number'],

// delimiter: after number because of .\d floats
[/[;,.]/, "delimiter"],
[/[=><!~?:&|+\-*/^%]+/, "operator"],

[/"([^"\\]|\\.)*$/, "string.invalid"],
[/"/, { token: "string.quote", bracket: "@open", next: "@string" }],

// characters
[/'[^\\']'/, 'string'],
[/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
[/'/, 'string.invalid']


],
comment: [
[/[^/*]+/, 'comment'],
[/\/\*/, 'comment', '@push'],
["\\*/", 'comment', '@pop'],
[/[/*]/, 'comment']
],

string: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }]
],

whitespace: [
[/[ \t\r\n]+/, 'white'],
[/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
],

bracketCounting: [
[/{/, 'delimiter.bracket', '@bracketCounting'],
[/}/, 'delimiter.bracket', '@pop'],
],
},
};
export const LanguageConfig: monaco.languages.LanguageConfiguration = {
// tokenize all words as identifiers
wordPattern: /(-?\d*\.\d\w*)|([^`~!@#%^&*()\-=+[{\]}\\|;:'",.<>/?\s]+)/g,
comments: {
lineComment: "//",
blockComment: ["/*", "*/"],
},
brackets: [
["{", "}"],
["[", "]"],
["(", ")"],
['"', '"'],
["'", "'"],
],
onEnterRules: [
// if current cursor is betwwen work and space,then will not indent
{
beforeText: /\w/,
afterText: /^$/,
action: { indentAction: monaco.languages.IndentAction.None },
},
{
// match /*
beforeText: /^\s*(\/\*)/,
// match */
afterText: /^\s*\*\/$/,
action: { indentAction: monaco.languages.IndentAction.IndentOutdent },
},
],
};
export {
keywords,
typeKeywords,
Expand Down
Loading

0 comments on commit 91f0f76

Please sign in to comment.