@@ -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 ) ;
1716const 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- / f u n c t i o n \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - 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 = / f u n c t i o n \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * ) \s * \( [ ^ ) ] * \) \s * \{ [ ^ ] * ?\} / g;
409+ break ;
410+ case 'py' :
411+ functionRegex = / d e f \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * ) \s * \( [ ^ ) ] * \) \s * : / g;
412+ break ;
413+ case 'java' :
414+ functionRegex = / (?: p u b l i c | p r o t e c t e d | p r i v a t e | s t a t i c | \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 = / f u n c t i o n \s + ( [ a - z A - Z _ \x80 - \xff ] [ a - z A - Z 0 - 9 _ \x80 - \xff ] * ) \s * \( [ ^ ) ] * \) \s * { / g;
422+ break ;
423+ case 'rb' :
424+ functionRegex = / d e f \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * ) \s * \( [ ^ ) ] * \) \s * / g;
425+ break ;
426+ case 'swift' :
427+ functionRegex = / f u n c \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * ) \s * \( [ ^ ) ] * \) \s * ( - > \s * \w + \s * ) ? \{ / g;
428+ break ;
429+ case 'cs' :
430+ functionRegex = / (?: p u b l i c | p r i v a t e | p r o t e c t e d | i n t e r n a l | s t a t i c ) \s + (?: a s y n c \s + ) ? (?: \w + \s + ) ? ( \w + ) \s * \( [ ^ ) ] * \) \s * { / g;
431+ break ;
432+ case 'go' :
433+ functionRegex = / f u n c \s + \( \s * [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * \s * \w * \s * \) \s + ( \w + ) \s * \( [ ^ ) ] * \) \s * { / g;
434+ break ;
435+ case 'lua' :
436+ functionRegex = / f u n c t i o n \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * ) \s * \( [ ^ ) ] * \) \s * { / g;
437+ break ;
438+ case 'perl' :
439+ functionRegex = / s u b \s + ( [ a - z A - Z _ $ ] [ 0 - 9 a - z A - 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+
413469function moveCursorToTryBlock ( editor , position ) {
414470 // Define the line where the try block starts (adjust as needed)
415471 const tryBlockStartLine = position . line + 2 ;
0 commit comments