|
| 1 | +import * as vscode from 'vscode'; |
| 2 | + |
| 3 | +import { ConfigManager } from '@/configuration/ConfigManager'; |
| 4 | +import { logger } from '@/extension'; |
| 5 | +import { LanguageFunctionCommentSettings } from '@/typings/types'; |
| 6 | +import { escapeRegexString } from '@/utils/str'; |
| 7 | + |
| 8 | +import { FunctionParamsParser } from './FunctionParamsParser'; |
| 9 | +import { splitParams } from './c-splitParams'; |
| 10 | +import { extractFunctionParamsString } from './extractFunctionParamsString'; |
| 11 | +import { FunctionParamsInfo, ParamsInfo, ReturnInfo } from './types'; |
| 12 | + |
| 13 | +function matchNormalFunction( |
| 14 | + functionDefinition: string, |
| 15 | + languageSettings: LanguageFunctionCommentSettings, |
| 16 | +): { |
| 17 | + matched: boolean; |
| 18 | + returnType: ReturnInfo; |
| 19 | + params: ParamsInfo; |
| 20 | +} { |
| 21 | + const { defaultReturnName = 'default' } = languageSettings; |
| 22 | + const returnType: ReturnInfo = {}; |
| 23 | + let matched = false; |
| 24 | + let params: ParamsInfo = {}; |
| 25 | + |
| 26 | + // 提取参数括号里的字符串 |
| 27 | + const functionParamsStr = extractFunctionParamsString(functionDefinition); |
| 28 | + const functionParamsRegStr = escapeRegexString(functionParamsStr); |
| 29 | + const functionPattern = new RegExp( |
| 30 | + `(.*?)\\s+([a-zA-Z0-9_]+)\\s*\\(${functionParamsRegStr}\\)\\s*{[\\s\\S]*?}`, |
| 31 | + 'm', |
| 32 | + ); |
| 33 | + |
| 34 | + const match = functionPattern.exec(functionDefinition); |
| 35 | + const modifiers = ['static', 'inline']; |
| 36 | + |
| 37 | + if (match) { |
| 38 | + matched = true; |
| 39 | + const prefixParts = match[1].split(' ').filter((part) => part !== ''); |
| 40 | + let returnTypeIndex = 0; |
| 41 | + |
| 42 | + for (let i = 0; i < prefixParts.length; i++) { |
| 43 | + if (!modifiers.includes(prefixParts[i])) { |
| 44 | + returnTypeIndex = i; |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const returnTypeStr = prefixParts.slice(returnTypeIndex).join(' '); |
| 50 | + returnType[defaultReturnName] = { |
| 51 | + type: returnTypeStr, |
| 52 | + description: '', |
| 53 | + }; |
| 54 | + |
| 55 | + params = splitParams(functionParamsStr, languageSettings); |
| 56 | + } |
| 57 | + |
| 58 | + return { matched, returnType, params }; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @description |
| 63 | + * @return default {auto} |
| 64 | + */ |
| 65 | +function matchFunction( |
| 66 | + functionDefinition: string, |
| 67 | + languageSettings: LanguageFunctionCommentSettings, |
| 68 | +): { matched: boolean; returnType: ReturnInfo; params: ParamsInfo } { |
| 69 | + const { defaultReturnName = 'default', defaultReturnType = 'auto' } = languageSettings; |
| 70 | + let returnType: ReturnInfo = { |
| 71 | + [defaultReturnName]: { type: defaultReturnType, description: '' }, |
| 72 | + }; |
| 73 | + let matched = false; |
| 74 | + let params: ParamsInfo = {}; |
| 75 | + |
| 76 | + const matchers = [matchNormalFunction]; |
| 77 | + |
| 78 | + for (const matcher of matchers) { |
| 79 | + const result = matcher(functionDefinition, languageSettings); |
| 80 | + if (result.matched) { |
| 81 | + matched = result.matched; |
| 82 | + params = result.params; |
| 83 | + returnType = result.returnType; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return { matched, returnType, params }; |
| 89 | +} |
| 90 | + |
| 91 | +export class CParser extends FunctionParamsParser { |
| 92 | + constructor(configManager: ConfigManager, languageId: string) { |
| 93 | + super(configManager, languageId); |
| 94 | + } |
| 95 | + |
| 96 | + private getFunctionString(document: vscode.TextDocument, startLine: number) { |
| 97 | + let functionDefinition = ''; |
| 98 | + let bracketCount = 0; // 大括号计数 |
| 99 | + let parenthesisCount = 0; // 小括号计数 |
| 100 | + |
| 101 | + for (let i = startLine; i < document.lineCount; i++) { |
| 102 | + const line = document.lineAt(i); |
| 103 | + functionDefinition += line.text + '\n'; |
| 104 | + |
| 105 | + for (const char of line.text) { |
| 106 | + if (char === '(') { |
| 107 | + parenthesisCount++; |
| 108 | + } else if (char === ')') { |
| 109 | + parenthesisCount--; |
| 110 | + } else if (char === '{') { |
| 111 | + bracketCount++; |
| 112 | + } else if (char === '}') { |
| 113 | + bracketCount--; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (bracketCount === 0 && parenthesisCount === 0) { |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return functionDefinition; |
| 123 | + } |
| 124 | + |
| 125 | + public getFunctionParamsAtCursor( |
| 126 | + activeEditor: vscode.TextEditor, |
| 127 | + languageSettings: LanguageFunctionCommentSettings = this.languageSettings, |
| 128 | + ): FunctionParamsInfo { |
| 129 | + let functionParams: ParamsInfo = {}; |
| 130 | + let matchedFunction = false; |
| 131 | + let returnType: ReturnInfo = {}; |
| 132 | + const document = activeEditor.document; |
| 133 | + const cursorLine = activeEditor.selection.start.line; |
| 134 | + let startLine = cursorLine; |
| 135 | + // 如果光标所在行为空行或者注释,则从下一行开始 |
| 136 | + const cursorLineText = document.lineAt(cursorLine).text.trim(); |
| 137 | + if (cursorLineText === '' || cursorLineText === '//' || cursorLineText === '*/') { |
| 138 | + startLine = cursorLine + 1; |
| 139 | + } |
| 140 | + |
| 141 | + const functionDefinition = this.getFunctionString(document, startLine); |
| 142 | + const { |
| 143 | + matched, |
| 144 | + returnType: returnTypeTmp, |
| 145 | + params, |
| 146 | + } = matchFunction(functionDefinition, languageSettings); |
| 147 | + if (matched) { |
| 148 | + matchedFunction = true; |
| 149 | + returnType = returnTypeTmp; |
| 150 | + functionParams = params; |
| 151 | + } |
| 152 | + |
| 153 | + if (!matchFunction) { |
| 154 | + logger.info(vscode.l10n.t('No function found at the cursor')); |
| 155 | + } |
| 156 | + |
| 157 | + return { |
| 158 | + matchedFunction, |
| 159 | + returnType, |
| 160 | + params: functionParams, |
| 161 | + insertPosition: new vscode.Position(startLine, 0), |
| 162 | + }; |
| 163 | + } |
| 164 | +} |
0 commit comments