-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0a76ec
commit 5a8c95e
Showing
12 changed files
with
321 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React, { Suspense } from 'react'; | ||
|
||
import { CommonEditorProps } from '../types'; | ||
|
||
const MonacoEditorLazy = React.lazy(() => import('./MonacoEditorCore')); | ||
|
||
const MonacoEditor: React.SFC<CommonEditorProps> = props => ( | ||
<Suspense fallback={'Loading'}> | ||
<MonacoEditorLazy {...props} /> | ||
</Suspense> | ||
) | ||
|
||
export default MonacoEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import { CommonEditorProps } from '../types'; | ||
import MonacoEditor, { EditorWillMount } from 'react-monaco-editor'; | ||
import { useSelector } from 'react-redux'; | ||
import State from '../state'; | ||
import { config, grammar, themeVsDarkPlus } from './rust_monaco_def'; | ||
|
||
import styles from './Editor.module.css'; | ||
|
||
const MODE_ID = 'my-rust'; | ||
|
||
const initMonaco: EditorWillMount = (monaco) => { | ||
monaco.editor.defineTheme('vscode-dark-plus', themeVsDarkPlus); | ||
|
||
monaco.languages.register({ | ||
id: MODE_ID, | ||
}); | ||
|
||
monaco.languages.onLanguage(MODE_ID, async () => { | ||
monaco.languages.setLanguageConfiguration(MODE_ID, config); | ||
monaco.languages.setMonarchTokensProvider(MODE_ID, grammar); | ||
}); | ||
}; | ||
|
||
const MonacoEditorCore: React.SFC<CommonEditorProps> = props => { | ||
const theme = useSelector((s: State) => s.configuration.monaco.theme); | ||
|
||
return ( | ||
<MonacoEditor | ||
language={MODE_ID} | ||
theme={theme} | ||
className={styles.monaco} | ||
value={props.code} | ||
onChange={props.onEditCode} | ||
editorWillMount={initMonaco} | ||
options={{ automaticLayout: true }} | ||
/> | ||
); | ||
} | ||
|
||
export default MonacoEditorCore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { languages, editor } from 'monaco-editor'; | ||
|
||
export const config: languages.LanguageConfiguration = { | ||
comments: { | ||
lineComment: '//', | ||
blockComment: ['/*', '*/'], | ||
}, | ||
brackets: [ | ||
['{', '}'], | ||
['[', ']'], | ||
['(', ')'], | ||
], | ||
autoClosingPairs: [ | ||
{ open: '[', close: ']' }, | ||
{ open: '{', close: '}' }, | ||
{ open: '(', close: ')' }, | ||
{ open: '"', close: '"', notIn: ['string'] }, | ||
], | ||
surroundingPairs: [ | ||
{ open: '{', close: '}' }, | ||
{ open: '[', close: ']' }, | ||
{ open: '(', close: ')' }, | ||
{ open: '"', close: '"' }, | ||
{ open: '\'', close: '\'' }, | ||
], | ||
folding: { | ||
markers: { | ||
start: new RegExp('^\\s*#pragma\\s+region\\b'), | ||
end: new RegExp('^\\s*#pragma\\s+endregion\\b'), | ||
}, | ||
}, | ||
}; | ||
|
||
export const grammar: languages.IMonarchLanguage = { | ||
// Set defaultToken to invalid to see what you do not tokenize yet | ||
// defaultToken: 'invalid', | ||
|
||
keywords: [ | ||
'as', 'break', 'const', 'crate', 'enum', 'extern', 'false', 'fn', 'impl', 'in', | ||
'let', 'mod', 'move', 'mut', 'pub', 'ref', 'return', 'self', 'Self', 'static', | ||
'struct', 'super', 'trait', 'true', 'type', 'unsafe', 'use', 'where', | ||
'macro_rules', | ||
], | ||
|
||
controlFlowKeywords: [ | ||
'continue', 'else', 'for', 'if', 'while', 'loop', 'match', | ||
], | ||
|
||
typeKeywords: [ | ||
'Self', 'm32', 'm64', 'm128', 'f80', 'f16', 'f128', 'int', 'uint', 'float', 'char', | ||
'bool', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64', 'i8', 'i16', 'i32', 'i64', 'str', | ||
'Option', 'Either', 'c_float', 'c_double', 'c_void', 'FILE', 'fpos_t', 'DIR', 'dirent', | ||
'c_char', 'c_schar', 'c_uchar', 'c_short', 'c_ushort', 'c_int', 'c_uint', 'c_long', 'c_ulong', | ||
'size_t', 'ptrdiff_t', 'clock_t', 'time_t', 'c_longlong', 'c_ulonglong', 'intptr_t', | ||
'uintptr_t', 'off_t', 'dev_t', 'ino_t', 'pid_t', 'mode_t', 'ssize_t', | ||
], | ||
|
||
operators: [ | ||
'=', '>', '<', '!', '~', '?', ':', '==', '<=', '>=', '!=', | ||
'&&', '||', '++', '--', '+', '-', '*', '/', '&', '|', '^', '%', | ||
'<<', '>>', '>>>', '+=', '-=', '*=', '/=', '&=', '|=', '^=', | ||
'%=', '<<=', '>>=', '>>>=', | ||
], | ||
|
||
// we include these common regular expressions | ||
symbols: /[=><!~?:&|+\-*\/\^%]+/, | ||
|
||
// for strings | ||
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, | ||
|
||
// The main tokenizer for our languages | ||
tokenizer: { | ||
root: [ | ||
// identifiers and keywords | ||
[/[a-z_$][\w$]*/, { | ||
cases: { | ||
'@typeKeywords': 'type.identifier', | ||
'@keywords': { | ||
cases: { | ||
'fn': { token: 'keyword', next: '@func_decl' }, | ||
'@default': 'keyword', | ||
}, | ||
}, | ||
'@controlFlowKeywords': 'keyword.control', | ||
'@default': 'variable', | ||
}, | ||
}], | ||
[/[A-Z][\w\$]*/, 'type.identifier'], // to show class names nicely | ||
|
||
// whitespace | ||
{ include: '@whitespace' }, | ||
|
||
// delimiters and operators | ||
[/[{}()\[\]]/, '@brackets'], | ||
[/[<>](?!@symbols)/, '@brackets'], | ||
[/@symbols/, { | ||
cases: { | ||
'@operators': 'operator', | ||
'@default': '', | ||
}, | ||
}], | ||
|
||
// @ annotations. | ||
// As an example, we emit a debugging log message on these tokens. | ||
// Note: message are supressed during the first load -- change some lines to see them. | ||
[/@\s*[a-zA-Z_\$][\w\$]*/, { token: 'annotation', log: 'annotation token: $0' }], | ||
|
||
// numbers | ||
[/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'], | ||
[/0[xX][0-9a-fA-F]+/, 'number.hex'], | ||
[/\d+/, 'number'], | ||
|
||
// delimiter: after number because of .\d floats | ||
[/[;,.]/, 'delimiter'], | ||
|
||
// strings | ||
[/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string | ||
[/"/, { token: 'string.quote', bracket: '@open', next: '@string' }], | ||
|
||
// characters | ||
[/'[^\\']'/, 'string'], | ||
[/(')(@escapes)(')/, ['string', 'string.escape', 'string']], | ||
[/'/, 'string.invalid'], | ||
], | ||
|
||
comment: [ | ||
[/[^\/*]+/, 'comment'], | ||
[/\/\*/, 'comment', '@push'], // nested comment | ||
['\\*/', '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'], | ||
], | ||
|
||
func_decl: [ | ||
[ | ||
/[a-z_$][\w$]*/, 'support.function', '@pop', | ||
], | ||
], | ||
}, | ||
}; | ||
|
||
export const themeVsDarkPlus: editor.IStandaloneThemeData = { | ||
base: 'vs-dark', | ||
inherit: true, | ||
colors: {}, | ||
rules: [ | ||
{ token: 'keyword.control', foreground: 'C586C0' }, | ||
{ token: 'variable', foreground: '9CDCFE' }, | ||
{ token: 'support.function', foreground: 'DCDCAA' }, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.