|
| 1 | +import * as monaco from 'monaco-editor' |
| 2 | +import { GoToken, isKeywordValueToken, tokenByOffset, tokenToString } from '../parser/tokens' |
| 3 | + |
| 4 | +const getLineTokens = (str: string, lang: string): monaco.Token[] | null => { |
| 5 | + try { |
| 6 | + const tokens = monaco.editor.tokenize(str, lang) |
| 7 | + return tokens[0] |
| 8 | + } catch (_) { |
| 9 | + return null |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +const checkPackageNameToken = (tokens: monaco.Token[], tokenPos: number) => { |
| 14 | + let identPos = -1 |
| 15 | + let foundIdent = false |
| 16 | + let foundDot = false |
| 17 | + |
| 18 | + // Ensure that there is no delimiters or identifiers behind |
| 19 | + // to ensure that it's a first identifier in a chain. |
| 20 | + // |
| 21 | + // Expected backward pattern is: |
| 22 | + // <packageName>. |
| 23 | + // |
| 24 | + // Necessary to reject cases like: |
| 25 | + // foo.bar.baz |
| 26 | + for (let i = tokenPos; i >= 0; i--) { |
| 27 | + const tok = tokens[i] |
| 28 | + switch (tok.type) { |
| 29 | + case GoToken.Dot: |
| 30 | + if (foundDot || foundIdent) { |
| 31 | + return -1 |
| 32 | + } |
| 33 | + |
| 34 | + foundDot = true |
| 35 | + break |
| 36 | + case GoToken.None: |
| 37 | + if (!foundIdent) { |
| 38 | + // no ident |
| 39 | + return -1 |
| 40 | + } |
| 41 | + |
| 42 | + // we don't expect dots anymore |
| 43 | + foundDot = true |
| 44 | + break |
| 45 | + case GoToken.Ident: |
| 46 | + if (foundIdent) { |
| 47 | + // twice ident |
| 48 | + return -1 |
| 49 | + } |
| 50 | + |
| 51 | + foundIdent = true |
| 52 | + identPos = i |
| 53 | + break |
| 54 | + case GoToken.Parenthesis: |
| 55 | + return foundIdent ? identPos : -1 |
| 56 | + default: |
| 57 | + // unexpected |
| 58 | + return -1 |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return identPos |
| 63 | +} |
| 64 | + |
| 65 | +export interface HoverValue { |
| 66 | + packageName?: string |
| 67 | + value: string |
| 68 | + range: { |
| 69 | + startColumn: number |
| 70 | + endColumn: number |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Resolves symbol query from a currently focused token. |
| 76 | + */ |
| 77 | +const resolveHoverValue = (line: string, tokens: monaco.Token[], tokenPos: number): HoverValue | null => { |
| 78 | + const hoverValue = tokenToString(line, tokens, tokenPos) |
| 79 | + const tok = tokens[tokenPos] |
| 80 | + const range = { |
| 81 | + startColumn: tok.offset + 1, |
| 82 | + endColumn: tok.offset + hoverValue.length + 1, |
| 83 | + } |
| 84 | + |
| 85 | + // check if there is a symbol behind to determine direction to move. |
| 86 | + const pkgNamePos = checkPackageNameToken(tokens, tokenPos - 1) |
| 87 | + if (pkgNamePos !== -1) { |
| 88 | + const pkgName = tokenToString(line, tokens, pkgNamePos) |
| 89 | + range.startColumn = tokens[pkgNamePos].offset + 1 |
| 90 | + return { |
| 91 | + packageName: pkgName, |
| 92 | + value: hoverValue, |
| 93 | + range, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return { value: hoverValue, range } |
| 98 | +} |
| 99 | + |
| 100 | +const keywordHoverValue = (line: string, tokens: monaco.Token[], tokenPos: number): HoverValue => { |
| 101 | + const value = tokenToString(line, tokens, tokenPos) |
| 102 | + const { offset } = tokens[tokenPos] |
| 103 | + return { |
| 104 | + value, |
| 105 | + range: { |
| 106 | + startColumn: offset + 1, |
| 107 | + endColumn: offset + value.length + 1, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export const queryFromPosition = ( |
| 113 | + model: monaco.editor.ITextModel, |
| 114 | + { lineNumber, column }: monaco.IPosition, |
| 115 | +): HoverValue | null => { |
| 116 | + const line = model.getLineContent(lineNumber) |
| 117 | + const tokens = getLineTokens(line, model.getLanguageId()) |
| 118 | + if (!tokens) { |
| 119 | + return null |
| 120 | + } |
| 121 | + |
| 122 | + const offset = column - 1 |
| 123 | + const r = tokenByOffset(tokens, offset, line.length) |
| 124 | + if (!r) { |
| 125 | + return null |
| 126 | + } |
| 127 | + |
| 128 | + const { tok, index } = r |
| 129 | + if (tok.type === GoToken.Ident) { |
| 130 | + return resolveHoverValue(line, tokens, index) |
| 131 | + } |
| 132 | + |
| 133 | + if (isKeywordValueToken(tok.type)) { |
| 134 | + return keywordHoverValue(line, tokens, index) |
| 135 | + } |
| 136 | + |
| 137 | + if (tok.type !== GoToken.Ident) { |
| 138 | + return null |
| 139 | + } |
| 140 | + |
| 141 | + return resolveHoverValue(line, tokens, index) |
| 142 | +} |
0 commit comments