Skip to content

Commit 1aa19d6

Browse files
author
sakuraash
committed
skipped tokenizer for lines longer than 10k characters
1 parent b383859 commit 1aa19d6

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

app/components/MonacoEditor/MonacoReact/grammars/set-grammars.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import config from 'config'
2+
import { notify, NOTIFY_TYPE } from 'components/Notification/actions'
13
import languagesConfig from './languages'
24
import { INITIAL } from './monaco-textmate'
35

@@ -43,15 +45,39 @@ export function wireTmGrammars (monaco, registry, languages) {
4345
monaco.languages.setTokensProvider(languageId, {
4446
getInitialState: () => new TokenizerState(INITIAL),
4547
tokenize: (line, state) => {
48+
if (line.length > 10000) {
49+
if (!config.tokenizationWarningAlreadyShown) {
50+
console.warn('Too many characters! Tokenization is skipped for lines longer than 10k characters for performance reasons.')
51+
notify({ message: i18n`editor.tokenizationWarning`, notifyType: NOTIFY_TYPE.ERROR })
52+
config.tokenizationWarningAlreadyShown = true
53+
}
54+
const tokens = new Uint32Array(2)
55+
tokens[0] = 0
56+
tokens[1] = (
57+
(1 << 0)
58+
| (0 << 8)
59+
| (0 << 11)
60+
| (1 << 14)
61+
| (2 << 23)
62+
) >>> 0;
63+
return {
64+
endState: new TokenizerState(state.ruleStack),
65+
tokens: [{
66+
startIndex: 0,
67+
scopes: '',
68+
endIndex: line.length,
69+
}],
70+
}
71+
}
4672
const res = grammar.tokenizeLine(line, state.ruleStack)
47-
return {
73+
const tokenize = {
4874
endState: new TokenizerState(res.ruleStack),
4975
tokens: res.tokens.map(token => ({
5076
...token,
51-
// TODO: At the moment, monaco-editor doesn't seem to accept array of scopes
5277
scopes: token.scopes[token.scopes.length - 1]
5378
}))
5479
}
80+
return tokenize
5581
}
5682
})
5783
} catch (e) {

app/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const config = observable({
4141
// 是否为插件开发工作空间
4242
__PLUGIN_DEV__: false,
4343
fileicons: 'default',
44+
tokenizationWarningAlreadyShown: false,
4445
tencentOrigin: 'https://studio.dev.tencent.com',
4546
get previewURL () {
4647
if (config.staticServingToken && config.spaceKey && config.staticServingURL) {

app/i18n/en_US/editor.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tokenizationWarning": "Tokenization is skipped for lines longer than 10k characters for performance reasons."
3+
}

app/i18n/en_US/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const contents = ['menuBarItems', 'settings', 'file', 'panel', 'tab', 'git', 'fileTree', 'global', 'modal', 'login', 'import', 'fileTreeTool', 'commandPalette', 'project', 'monaco', 'plugin'];
1+
const contents = ['menuBarItems', 'settings', 'file', 'panel', 'tab', 'git', 'fileTree', 'global', 'modal', 'login', 'import', 'fileTreeTool', 'commandPalette', 'project', 'monaco', 'plugin', 'editor'];
22

33
export default contents.reduce((p, v) => {
44
p[v] = require(`./${v}.json`)

app/i18n/zh_CN/editor.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tokenizationWarning": "由于性能原因,将不对单行大于1万字符的文件进行标记化解析."
3+
}

app/i18n/zh_CN/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const contents = ['menuBarItems', 'settings', 'file', 'panel', 'tab', 'git', 'fileTree', 'global', 'modal', 'login', 'import', 'fileTreeTool', 'commandPalette', 'project', 'monaco', 'plugin'];
1+
const contents = ['menuBarItems', 'settings', 'file', 'panel', 'tab', 'git', 'fileTree', 'global', 'modal', 'login', 'import', 'fileTreeTool', 'commandPalette', 'project', 'monaco', 'plugin', 'editor'];
22

33
export default contents.reduce((p, v) => {
44
p[v] = require(`./${v}.json`)

0 commit comments

Comments
 (0)