Skip to content

Commit

Permalink
use a general method to support minified monaco
Browse files Browse the repository at this point in the history
  • Loading branch information
troy351 committed Mar 6, 2023
1 parent 06c4e19 commit 7e3f116
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -34,8 +34,8 @@
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.0.0",
"monaco-editor": "^0.35.0",
"rollup": "^3.15.0",
"monaco-editor": "^0.36.1",
"rollup": "^3.18.0",
"tslib": "^2.5.0",
"typescript": "^4.9.5"
}
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 39 additions & 20 deletions src/abbreviationActions.ts
Expand Up @@ -35,6 +35,42 @@ function isValidEmmetToken(tokens: Token[], index: number, syntax: string, langu
return false
}

const tokenEnvCache = new WeakMap<any, { _stateStore: any; _support: any }>()
function getTokenizationEnv(model: any) {
if (tokenEnvCache.has(model)) return tokenEnvCache.get(model)!

let _tokenization =
// monaco-editor < 0.34.0
model._tokenization ||
// monaco-editor 0.34.0
model.tokenization._tokenization

// monaco-editor <= 0.34.0
let _tokenizationStateStore = _tokenization?._tokenizationStateStore

// monaco-editor >= 0.35.0, source code was minified
if (!_tokenization || !_tokenizationStateStore) {
const _t = model.tokenization

Object.values(_t).some((val: any) => (_tokenization = val.tokenizeViewport && val))
Object.values(_tokenization).some((val: any) => (_tokenizationStateStore = val.tokenizationSupport && val))
}

const _tokenizationSupport =
// monaco-editor >= 0.32.0
_tokenizationStateStore.tokenizationSupport ||
// monaco-editor < 0.32.0
_tokenization._tokenizationSupport

const env = {
_stateStore: _tokenizationStateStore,
_support: _tokenizationSupport,
}

tokenEnvCache.set(model, env)
return env
}

// vscode did a complex node analysis, we just use monaco's built-in tokenizer
// to achieve almost the same effect
export function isValidLocationForEmmetAbbreviation(
Expand All @@ -46,26 +82,9 @@ export function isValidLocationForEmmetAbbreviation(
const { column, lineNumber } = position

// get current line's tokens
const _tokenization =
// monaco-editor < 0.34.0
(model as any)._tokenization ||
// monaco-editor 0.34.0
(model as any).tokenization._tokenization ||
// monaco-editor >= 0.35.0
(model as any).tokenization.m
const _tokenizationStateStore =
// monaco-editor <= 0.34.0
_tokenization._tokenizationStateStore ||
// monaco-editor >= 0.35.0
_tokenization.a
const _tokenizationSupport =
// monaco-editor >= 0.32.0
_tokenizationStateStore.tokenizationSupport ||
// monaco-editor < 0.32.0
_tokenization._tokenizationSupport

const state = _tokenizationStateStore.getBeginState(lineNumber - 1).clone()
const tokenizationResult = _tokenizationSupport.tokenize(model.getLineContent(lineNumber), true, state, 0)
const { _stateStore, _support } = getTokenizationEnv(model)
const state = _stateStore.getBeginState(lineNumber - 1).clone()
const tokenizationResult = _support.tokenize(model.getLineContent(lineNumber), true, state, 0)
const tokens: Token[] = tokenizationResult.tokens

let valid = false
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2016",
"target": "ES2017",
"module": "esnext",
"strict": true,
"jsx": "preserve",
Expand Down

0 comments on commit 7e3f116

Please sign in to comment.