Skip to content

Commit ba3fb1c

Browse files
update extension.js
1 parent 847ee12 commit ba3fb1c

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

extension.js

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ function activate(context) {
1212
const document = editor.document;
1313
const position = editor.selection.active;
1414

15-
const functionName = getEnclosingFunctionName(document, position);
1615
const fileName = path.basename(document.fileName);
1716
const fileExtension = getFileExtension(fileName);
17+
const functionName = getEnclosingFunctionName(document, position, fileExtension);
1818

1919
// Insert the try-catch snippet
2020
editor
@@ -390,26 +390,82 @@ function getFileExtension(fileName) {
390390
}
391391

392392

393-
function getEnclosingFunctionName(document, position) {
394-
const text = document.getText();
395-
const functionRegex =
396-
/function\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*\{[^]*?\}/g;
397-
let match;
398-
let functionName = null;
393+
function getEnclosingFunctionName(document, position, fileExtension) {
394+
const text = document.getText();
395+
let functionRegex;
399396

400-
while ((match = functionRegex.exec(text)) !== null) {
401-
const startPos = document.positionAt(match.index);
402-
const endPos = document.positionAt(match.index + match[0].length);
397+
if (!fileExtension) {
398+
return null; // Return null if fileExtension is not provided or falsy
399+
}
400+
401+
fileExtension = fileExtension.toLowerCase();
402+
403+
switch (fileExtension) {
404+
case 'js':
405+
case 'ts':
406+
case 'jsx':
407+
case 'tsx':
408+
functionRegex = /function\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*\{[^]*?\}/g;
409+
break;
410+
case 'py':
411+
functionRegex = /def\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*:/g;
412+
break;
413+
case 'java':
414+
functionRegex = /(?:public|protected|private|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])/g;
415+
break;
416+
case 'c':
417+
case 'cpp':
418+
functionRegex = /(?:[\w\s*]+)\s+(\w+)\s*\([^)]*\)\s*{/g;
419+
break;
420+
case 'php':
421+
functionRegex = /function\s+([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\s*\([^)]*\)\s*{/g;
422+
break;
423+
case 'rb':
424+
functionRegex = /def\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*/g;
425+
break;
426+
case 'swift':
427+
functionRegex = /func\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*(->\s*\w+\s*)?\{/g;
428+
break;
429+
case 'cs':
430+
functionRegex = /(?:public|private|protected|internal|static)\s+(?:async\s+)?(?:\w+\s+)?(\w+)\s*\([^)]*\)\s*{/g;
431+
break;
432+
case 'go':
433+
functionRegex = /func\s+\(\s*[a-zA-Z_$][0-9a-zA-Z_$]*\s*\w*\s*\)\s+(\w+)\s*\([^)]*\)\s*{/g;
434+
break;
435+
case 'lua':
436+
functionRegex = /function\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*{/g;
437+
break;
438+
case 'perl':
439+
functionRegex = /sub\s+([a-zA-Z_$][0-9a-zA-Z_$]*)\s*\([^)]*\)\s*{/g;
440+
break;
441+
// Add more cases as needed for other languages
442+
default:
443+
functionRegex = null;
444+
break;
445+
}
403446

404-
if (position.isAfterOrEqual(startPos) && position.isBeforeOrEqual(endPos)) {
405-
functionName = match[1];
406-
break;
447+
if (!functionRegex) {
448+
return null; // Return null if no matching regex pattern found
407449
}
408-
}
409450

410-
return functionName;
451+
let match;
452+
let functionName = null;
453+
454+
while ((match = functionRegex.exec(text)) !== null) {
455+
const startPos = document.positionAt(match.index);
456+
const endPos = document.positionAt(match.index + match[0].length);
457+
458+
if (position.isAfterOrEqual(startPos) && position.isBeforeOrEqual(endPos)) {
459+
functionName = match[1];
460+
break;
461+
}
462+
}
463+
464+
return functionName;
411465
}
412466

467+
468+
413469
function moveCursorToTryBlock(editor, position) {
414470
// Define the line where the try block starts (adjust as needed)
415471
const tryBlockStartLine = position.line + 2;

0 commit comments

Comments
 (0)