-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: ygqygq2 <[email protected]>
- Loading branch information
Showing
28 changed files
with
462 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
sampleWorkspace/function-comment-for-python/function-none-params-with-return.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def funcName () -> str: | ||
return "test" |
6 changes: 6 additions & 0 deletions
6
sampleWorkspace/function-comment-for-python/function-none-params-with-return.result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
* @description | ||
* @return default {str} | ||
""" | ||
def funcName () -> str: | ||
return "test" |
2 changes: 2 additions & 0 deletions
2
sampleWorkspace/function-comment-for-python/function-none-params-without-return.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def funcName (): | ||
return "test" |
6 changes: 6 additions & 0 deletions
6
sampleWorkspace/function-comment-for-python/function-none-params-without-return.result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
* @description | ||
* @return default {auto} | ||
""" | ||
def funcName (): | ||
return "test" |
2 changes: 2 additions & 0 deletions
2
sampleWorkspace/function-comment-for-python/function-optional-params-with-return.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def add(a='a', **kwargs) -> Union[int, float]: | ||
return a + b |
8 changes: 8 additions & 0 deletions
8
sampleWorkspace/function-comment-for-python/function-optional-params-with-return.result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
""" | ||
* @description | ||
* @return default {Union[int, float]} | ||
* @param [a='a'] {any} | ||
* @param [kwargs] {any} | ||
""" | ||
def add(a='a', **kwargs) -> Union[int, float]: | ||
return a + b |
10 changes: 10 additions & 0 deletions
10
sampleWorkspace/function-comment-for-python/function-with-params-with-return.result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @description | ||
* @return default {int} | ||
* @param String {a} | ||
* @param int {b} | ||
*/ | ||
public int func(String a, int b) { | ||
System.out.println("test"); | ||
return 1; | ||
} |
2 changes: 2 additions & 0 deletions
2
sampleWorkspace/function-comment-for-python/function-with-params-without-return.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def add(a: Union[int, float], b: Union[int, float]) -> Union[int, float]: | ||
return a + b |
8 changes: 8 additions & 0 deletions
8
sampleWorkspace/function-comment-for-python/function-with-params-without-return.result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
""" | ||
* @description | ||
* @return default {Union[int, float]} | ||
* @param a {Union[int, float]} | ||
* @param b {Union[int, float]} | ||
""" | ||
def add(a: Union[int, float], b: Union[int, float]) -> Union[int, float]: | ||
return a + b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
import { ConfigManager } from '@/configuration/ConfigManager'; | ||
import { logger } from '@/extension'; | ||
import { LanguageFunctionCommentSettings } from '@/typings/types'; | ||
import { escapeRegexString } from '@/utils/str'; | ||
|
||
import { extractFunctionParamsString } from './extractFunctionParamsString'; | ||
import { FunctionParamsParser } from './FunctionParamsParser'; | ||
import { splitParams } from './python-splitParams'; | ||
import { FunctionParamsInfo, ParamsInfo, ReturnInfo } from './types'; | ||
|
||
function matchNormalFunction( | ||
functionDefinition: string, | ||
languageSettings: LanguageFunctionCommentSettings, | ||
): { | ||
matched: boolean; | ||
returnType: ReturnInfo; | ||
params: ParamsInfo; | ||
} { | ||
const { defaultReturnName = 'default', defaultReturnType = 'Any' } = languageSettings; | ||
const returnType: ReturnInfo = {}; | ||
let matched = false; | ||
let params: ParamsInfo = {}; | ||
|
||
// 提取参数括号里的字符串 | ||
const functionParamsStr = extractFunctionParamsString(functionDefinition); | ||
const functionParamsRegStr = escapeRegexString(functionParamsStr); | ||
const functionPattern = new RegExp( | ||
`def\\s+([a-zA-Z0-9_]+)\\s*\\(${functionParamsRegStr}\\)\\s*(->\\s*(.*))?\\s*:`, | ||
'm', | ||
); | ||
|
||
const match = functionPattern.exec(functionDefinition); | ||
|
||
if (match) { | ||
matched = true; | ||
const returnTypeStr = match[3] ? match[3].trim() : defaultReturnType; | ||
|
||
returnType[defaultReturnName] = { | ||
type: returnTypeStr, | ||
description: '', | ||
}; | ||
|
||
params = splitParams(functionParamsStr, languageSettings); | ||
} | ||
|
||
return { matched, returnType, params }; | ||
} | ||
|
||
/** | ||
* @description | ||
* @return default {auto} | ||
*/ | ||
function matchFunction( | ||
functionDefinition: string, | ||
languageSettings: LanguageFunctionCommentSettings, | ||
): { matched: boolean; returnType: ReturnInfo; params: ParamsInfo } { | ||
const { defaultReturnName = 'default', defaultReturnType = 'Any' } = languageSettings; | ||
let returnType: ReturnInfo = { | ||
[defaultReturnName]: { type: defaultReturnType, description: '' }, | ||
}; | ||
let matched = false; | ||
let params: ParamsInfo = {}; | ||
|
||
const matchers = [matchNormalFunction]; | ||
|
||
for (const matcher of matchers) { | ||
const result = matcher(functionDefinition, languageSettings); | ||
if (result.matched) { | ||
matched = result.matched; | ||
params = result.params; | ||
returnType = result.returnType; | ||
break; | ||
} | ||
} | ||
|
||
return { matched, returnType, params }; | ||
} | ||
|
||
export class PythonParser extends FunctionParamsParser { | ||
constructor(configManager: ConfigManager, languageId: string) { | ||
super(configManager, languageId); | ||
} | ||
|
||
private getFunctionString(document: vscode.TextDocument, startLine: number) { | ||
let functionDefinition = ''; | ||
let bracketCount = 0; // 大括号计数 | ||
let parenthesisCount = 0; // 小括号计数 | ||
let colonDetected = false; // 函数冒号检测 | ||
|
||
for (let i = startLine; i < document.lineCount; i++) { | ||
const line = document.lineAt(i); | ||
functionDefinition += line.text + '\n'; | ||
|
||
for (const char of line.text) { | ||
if (char === '(') { | ||
parenthesisCount++; | ||
} else if (char === ')') { | ||
parenthesisCount--; | ||
} else if (char === '{') { | ||
bracketCount++; | ||
} else if (char === '}') { | ||
bracketCount--; | ||
} else if (char === ':' && parenthesisCount === 0) { | ||
colonDetected = true; | ||
} | ||
} | ||
|
||
if (bracketCount === 0 && parenthesisCount === 0 && colonDetected) { | ||
break; | ||
} | ||
} | ||
|
||
return functionDefinition; | ||
} | ||
|
||
public getFunctionParamsAtCursor( | ||
activeEditor: vscode.TextEditor, | ||
languageSettings: LanguageFunctionCommentSettings = this.languageSettings, | ||
): FunctionParamsInfo { | ||
let functionParams: ParamsInfo = {}; | ||
let matchedFunction = false; | ||
let returnType: ReturnInfo = {}; | ||
const document = activeEditor.document; | ||
const cursorLine = activeEditor.selection.start.line; | ||
let startLine = cursorLine; | ||
// 如果光标所在行为空行或者注释,则从下一行开始 | ||
const cursorLineText = document.lineAt(cursorLine).text.trim(); | ||
if (cursorLineText === '' || cursorLineText === '//' || cursorLineText === '*/') { | ||
startLine = cursorLine + 1; | ||
} | ||
|
||
const functionDefinition = this.getFunctionString(document, startLine); | ||
const { | ||
matched, | ||
returnType: returnTypeTmp, | ||
params, | ||
} = matchFunction(functionDefinition, languageSettings); | ||
if (matched) { | ||
matchedFunction = true; | ||
returnType = returnTypeTmp; | ||
functionParams = params; | ||
} | ||
|
||
if (!matchFunction) { | ||
logger.info(vscode.l10n.t('No function found at the cursor')); | ||
} | ||
|
||
return { | ||
matchedFunction, | ||
returnType, | ||
params: functionParams, | ||
insertPosition: new vscode.Position(startLine, 0), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { LanguageFunctionCommentSettings } from '@/typings/types'; | ||
|
||
import { ParamsInfo } from './types'; | ||
|
||
export function splitParams( | ||
paramsStr: string, | ||
languageSettings: LanguageFunctionCommentSettings, | ||
): ParamsInfo { | ||
const { defaultParamType = 'Any', defaultReturnName = 'default' } = languageSettings; | ||
let bracketCount = 0; | ||
let paramStartIndex = 0; | ||
let defaultCount = 0; | ||
const params: ParamsInfo = {}; | ||
for (let i = 0; i < paramsStr?.length; i++) { | ||
const char = paramsStr[i]; | ||
if (char === '(' || char === '[' || char === '{' || char === '<') { | ||
bracketCount++; | ||
} else if (char === ')' || char === ']' || char === '}' || char === '>') { | ||
bracketCount--; | ||
} | ||
|
||
if ( | ||
(char === ',' && bracketCount === 0) || | ||
(i === paramsStr.length - 1 && bracketCount === 0) | ||
) { | ||
const paramStr = paramsStr | ||
.slice(paramStartIndex, i === paramsStr.length - 1 ? i + 1 : i) | ||
.trim(); | ||
|
||
const paramPattern = /^(\*\*|\*)?(\w+)?\s*(:\s*(.*?))?(=\s*(.*))?$/; | ||
const match = paramPattern.exec(paramStr); | ||
if (match) { | ||
const name = | ||
match[2] || | ||
(defaultCount > 0 ? `${defaultReturnName}${defaultCount++}` : defaultReturnName); | ||
const type = match[4] || defaultParamType; | ||
const optional = match[1] || match[6] ? { optional: true } : {}; | ||
const defaultValue = match[6] ? { defaultValue: match[6] } : {}; | ||
|
||
params[name] = { type, description: '', ...optional, ...defaultValue }; | ||
} | ||
paramStartIndex = i + 1; | ||
} | ||
} | ||
|
||
return params; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.