File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -72,26 +72,42 @@ export function activate(context: vscode.ExtensionContext) {
7272 context . subscriptions . push ( willSaveListener ) ;
7373}
7474
75+ const DEFAULT_COMMENT_SYNTAX_MAP : Record < string , string > = {
76+ '.mjs' : '//' ,
77+ '.rs' : '//' ,
78+ '.js' : '//' ,
79+ '.jsx' : '//' ,
80+ '.ts' : '//' ,
81+ '.tsx' : '//' ,
82+ '.py' : '#' ,
83+ '.tf' : '#' ,
84+ '.hcl' : '#' ,
85+ '.java' : '//' ,
86+ '.c' : '//' ,
87+ '.cpp' : '//' ,
88+ '.cs' : '//' ,
89+ '.rb' : '#' ,
90+ '.go' : '//' ,
91+ '.php' : '//' ,
92+ } ;
93+
7594function getCommentSyntax ( extension : string ) : string | null {
76- const commentSyntaxMap : { [ key : string ] : string } = {
77- '.mjs' : '//' ,
78- '.rs' : '//' ,
79- '.js' : '//' ,
80- '.jsx' : '//' ,
81- '.ts' : '//' ,
82- '.tsx' : '//' ,
83- '.py' : '#' ,
84- '.tf' : '#' ,
85- '.hcl' : '#' ,
86- '.java' : '//' ,
87- '.c' : '//' ,
88- '.cpp' : '//' ,
89- '.cs' : '//' ,
90- '.rb' : '#' ,
91- '.go' : '//' ,
92- '.php' : '//' ,
93- } ;
94- return commentSyntaxMap [ extension ] || null ;
95+ // Read user overrides each time to reflect settings changes immediately.
96+ const cfg = vscode . workspace . getConfiguration ( 'autopathcomment' ) ;
97+ const userMap = cfg . get < Record < string , string > > ( 'commentSyntaxMap' , { } ) ;
98+ const effective = { ...DEFAULT_COMMENT_SYNTAX_MAP , ...normalizeMap ( userMap ) } as Record < string , string > ;
99+ return effective [ extension ] || null ;
100+ }
101+
102+ function normalizeMap ( map : Record < string , string > | undefined ) : Record < string , string > {
103+ if ( ! map ) return { } ;
104+ const out : Record < string , string > = { } ;
105+ for ( const [ k , v ] of Object . entries ( map ) ) {
106+ if ( ! k ) continue ;
107+ const key = k . startsWith ( '.' ) ? k : `.${ k } ` ;
108+ if ( typeof v === 'string' && v . trim ( ) . length > 0 ) out [ key ] = v ;
109+ }
110+ return out ;
95111}
96112
97113export function deactivate ( ) { }
Original file line number Diff line number Diff line change 1+ import * as assert from 'assert' ;
2+ import * as vscode from 'vscode' ;
3+
4+ // eslint-disable-next-line @typescript-eslint/no-var-requires
5+ const ext = require ( '../../dist/extension.js' ) ;
6+
7+ suite ( 'Configuration override' , ( ) => {
8+ test ( 'user settings override default map' , async ( ) => {
9+ const cfg = vscode . workspace . getConfiguration ( 'autopathcomment' ) ;
10+ // Use a rarely used extension to avoid clashing with defaults
11+ await cfg . update ( 'commentSyntaxMap' , { '.foo' : '#' } , vscode . ConfigurationTarget . Global ) ;
12+ try {
13+ assert . strictEqual ( ext . getCommentSyntax ( '.foo' ) , '#' ) ;
14+ } finally {
15+ // Cleanup
16+ await cfg . update ( 'commentSyntaxMap' , undefined , vscode . ConfigurationTarget . Global ) ;
17+ }
18+ } ) ;
19+ } ) ;
20+
You can’t perform that action at this time.
0 commit comments