1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
| import * as vscode from 'vscode';
export class MyLangHoverProvider implements vscode.HoverProvider { async provideHover( document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken ): Promise<vscode.Hover | undefined> { const wordRange = document.getWordRangeAtPosition(position); if (!wordRange) { return undefined; } const word = document.getText(wordRange); const line = document.lineAt(position.line).text; const hoverInfo = await this.getHoverInfo(document, word, position, line); if (hoverInfo) { return new vscode.Hover(hoverInfo, wordRange); } return undefined; } private async getHoverInfo( document: vscode.TextDocument, word: string, position: vscode.Position, line: string ): Promise<vscode.MarkdownString | undefined> { if (this.isKeyword(word)) { return this.getKeywordHover(word); } if (this.isFunctionCall(line, word)) { return this.getFunctionHover(document, word); } if (this.isVariable(document, word, position)) { return this.getVariableHover(document, word, position); } if (this.isType(word)) { return this.getTypeHover(word); } if (this.isBuiltinFunction(word)) { return this.getBuiltinFunctionHover(word); } return undefined; } private isKeyword(word: string): boolean { const keywords = [ 'function', 'var', 'let', 'const', 'if', 'else', 'while', 'for', 'return', 'break', 'continue', 'class', 'interface', 'enum', 'import', 'export', 'async', 'await', 'try', 'catch', 'finally' ]; return keywords.includes(word); } private getKeywordHover(keyword: string): vscode.MarkdownString { const descriptions: Record<string, string> = { 'function': '用于声明函数的关键字', 'var': '声明变量(函数作用域)', 'let': '声明变量(块作用域)', 'const': '声明常量(块作用域)', 'if': '条件语句', 'else': '条件语句的否则分支', 'while': '循环语句', 'for': '循环语句', 'return': '函数返回语句', 'break': '跳出循环或switch语句', 'continue': '跳过当前循环迭代', 'class': '声明类', 'interface': '声明接口', 'enum': '声明枚举', 'import': '导入模块', 'export': '导出模块成员', 'async': '声明异步函数', 'await': '等待异步操作完成', 'try': '异常处理语句', 'catch': '捕获异常', 'finally': '异常处理的最终执行块' }; const description = descriptions[keyword] || '关键字'; const markdown = new vscode.MarkdownString(); markdown.appendCodeblock(`${keyword}`, 'mylang'); markdown.appendMarkdown(`**关键字:** ${keyword}\n\n`); markdown.appendMarkdown(`**描述:** ${description}\n\n`); const examples: Record<string, string> = { 'function': 'function myFunction(param1, param2) {\n return param1 + param2;\n}', 'if': 'if (condition) {\n // 执行代码\n}', 'for': 'for (let i = 0; i < 10; i++) {\n // 循环体\n}', 'class': 'class MyClass {\n constructor() {\n // 构造函数\n }\n}' }; if (examples[keyword]) { markdown.appendMarkdown(`**示例:**\n`); markdown.appendCodeblock(examples[keyword], 'mylang'); } return markdown; } private isFunctionCall(line: string, word: string): boolean { const functionCallPattern = new RegExp(`\\b${word}\\s*\\(`); return functionCallPattern.test(line); } private getFunctionHover(document: vscode.TextDocument, functionName: string): vscode.MarkdownString { const text = document.getText(); const functionDefPattern = new RegExp(`function\\s+${functionName}\\s*\\(([^)]*)\\)\\s*\\{`, 'g'); const match = functionDefPattern.exec(text); const markdown = new vscode.MarkdownString(); if (match) { const params = match[1]; markdown.appendCodeblock(`function ${functionName}(${params})`, 'mylang'); markdown.appendMarkdown(`**函数:** ${functionName}\n\n`); markdown.appendMarkdown(`**参数:** ${params || '无'}\n\n`); const functionComment = this.extractFunctionComment(text, match.index); if (functionComment) { markdown.appendMarkdown(`**描述:** ${functionComment}\n\n`); } } else { const builtinInfo = this.getBuiltinFunctionInfo(functionName); if (builtinInfo) { markdown.appendCodeblock(`${functionName}(${builtinInfo.params})`, 'mylang'); markdown.appendMarkdown(`**内置函数:** ${functionName}\n\n`); markdown.appendMarkdown(`**描述:** ${builtinInfo.description}\n\n`); markdown.appendMarkdown(`**返回值:** ${builtinInfo.returnType}\n\n`); } else { markdown.appendCodeblock(functionName, 'mylang'); markdown.appendMarkdown(`**函数:** ${functionName}\n\n`); markdown.appendMarkdown(`*未找到函数定义*`); } } return markdown; } private isVariable(document: vscode.TextDocument, word: string, position: vscode.Position): boolean { const text = document.getText(new vscode.Range(new vscode.Position(0, 0), position)); const variablePattern = new RegExp(`\\b(var|let|const)\\s+${word}\\b`); return variablePattern.test(text); } private getVariableHover( document: vscode.TextDocument, variableName: string, position: vscode.Position ): vscode.MarkdownString { const text = document.getText(new vscode.Range(new vscode.Position(0, 0), position)); const variablePattern = new RegExp(`\\b(var|let|const)\\s+${variableName}(\\s*=\\s*([^;\\n]+))?`, 'g'); const matches = Array.from(text.matchAll(variablePattern)); const markdown = new vscode.MarkdownString(); if (matches.length > 0) { const lastMatch = matches[matches.length - 1]; const [, declarationType, , initialValue] = lastMatch; markdown.appendCodeblock(`${declarationType} ${variableName}${initialValue ? ` = ${initialValue}` : ''}`, 'mylang'); markdown.appendMarkdown(`**变量:** ${variableName}\n\n`); markdown.appendMarkdown(`**类型:** ${declarationType}\n\n`); if (initialValue) { markdown.appendMarkdown(`**初始值:** ${initialValue.trim()}\n\n`); const inferredType = this.inferType(initialValue.trim()); if (inferredType) { markdown.appendMarkdown(`**推断类型:** ${inferredType}\n\n`); } } } else { markdown.appendCodeblock(variableName, 'mylang'); markdown.appendMarkdown(`**变量:** ${variableName}\n\n`); markdown.appendMarkdown(`*未找到变量声明*`); } return markdown; } private isType(word: string): boolean { const types = ['string', 'number', 'boolean', 'object', 'array', 'void', 'any']; return types.includes(word); } private getTypeHover(typeName: string): vscode.MarkdownString { const typeDescriptions: Record<string, string> = { 'string': '字符串类型 - 表示文本数据', 'number': '数字类型 - 表示整数或浮点数', 'boolean': '布尔类型 - 表示true或false', 'object': '对象类型 - 表示复合数据结构', 'array': '数组类型 - 表示有序的元素集合', 'void': 'void类型 - 表示无返回值', 'any': 'any类型 - 表示任意类型' }; const markdown = new vscode.MarkdownString(); markdown.appendCodeblock(typeName, 'mylang'); markdown.appendMarkdown(`**类型:** ${typeName}\n\n`); markdown.appendMarkdown(`**描述:** ${typeDescriptions[typeName]}\n\n`); return markdown; } private isBuiltinFunction(word: string): boolean { const builtinFunctions = ['console', 'Math', 'String', 'Array', 'Object', 'Date']; return builtinFunctions.some(builtin => word.startsWith(builtin)); } private getBuiltinFunctionHover(word: string): vscode.MarkdownString { const builtinInfo = this.getBuiltinFunctionInfo(word); const markdown = new vscode.MarkdownString(); markdown.appendCodeblock(word, 'mylang'); markdown.appendMarkdown(`**内置对象/方法:** ${word}\n\n`); if (builtinInfo) { markdown.appendMarkdown(`**描述:** ${builtinInfo.description}\n\n`); if (builtinInfo.params) { markdown.appendMarkdown(`**参数:** ${builtinInfo.params}\n\n`); } if (builtinInfo.returnType) { markdown.appendMarkdown(`**返回值:** ${builtinInfo.returnType}\n\n`); } } return markdown; } private getBuiltinFunctionInfo(name: string): { description: string; params?: string; returnType?: string; } | undefined { const builtins: Record<string, any> = { 'console.log': { description: '在控制台输出信息', params: 'message: any', returnType: 'void' }, 'Math.abs': { description: '返回数字的绝对值', params: 'x: number', returnType: 'number' }, 'Math.max': { description: '返回最大值', params: '...values: number[]', returnType: 'number' }, 'String.prototype.charAt': { description: '返回指定位置的字符', params: 'index: number', returnType: 'string' }, 'Array.prototype.push': { description: '向数组末尾添加元素', params: '...items: any[]', returnType: 'number' } }; return builtins[name]; } private extractFunctionComment(text: string, functionIndex: number): string | undefined { const beforeFunction = text.substring(0, functionIndex); const lines = beforeFunction.split('\n'); for (let i = lines.length - 1; i >= 0; i--) { const line = lines[i].trim(); if (line.startsWith('//')) { return line.substring(2).trim(); } else if (line.includes('/*') && line.includes('*/')) { const commentMatch = line.match(/\/\*(.*?)\*\//); if (commentMatch) { return commentMatch[1].trim(); } } else if (line && !line.startsWith('//') && !line.includes('/*')) { break; } } return undefined; } private inferType(value: string): string | undefined { value = value.trim(); if (value.startsWith('"') && value.endsWith('"')) { return 'string'; } if (value.startsWith("'") && value.endsWith("'")) { return 'string'; } if (/^\d+$/.test(value)) { return 'number'; } if (/^\d+\.\d+$/.test(value)) { return 'number'; } if (value === 'true' || value === 'false') { return 'boolean'; } if (value.startsWith('[') && value.endsWith(']')) { return 'array'; } if (value.startsWith('{') && value.endsWith('}')) { return 'object'; } return undefined; } }
|