@@ -50,18 +50,37 @@ export class SelectionProvider {
5050 endIndex : number ,
5151 document : vscode . TextDocument ,
5252 ) : { start : number ; end : number } | null {
53- // Get all valid candidates from finders
54- const candidates = [
55- findToken ( text , startIndex , endIndex , document ) ,
56- findNearestQuotePair ( text , startIndex , endIndex ) ,
57- findNearestScope ( text , startIndex , endIndex ) ,
58- findLineExpansion ( text , startIndex , endIndex , document ) ,
59- ] . filter ( ( c ) => ! ! c ) ;
53+ // Get all valid candidates from finders as a map
54+ const candidateMap : Record < string , { start : number ; end : number } | null > =
55+ {
56+ token : findToken ( text , startIndex , endIndex , document ) ,
57+ quote : findNearestQuotePair ( text , startIndex , endIndex ) ,
58+ scope : findNearestScope ( text , startIndex , endIndex ) ,
59+ line : findLineExpansion ( text , startIndex , endIndex , document ) ,
60+ } ;
6061
61- // Return the smallest valid expansion
62+ // Return the smallest valid expansion, with priority logic
6263 let best : { start : number ; end : number } | null = null ;
6364 let smallest = Infinity ;
64- for ( const candidate of candidates ) {
65+
66+ // Priority: if scope or quote candidate exists and line candidate is not fully contained, ignore line candidate
67+ const line = candidateMap . line ;
68+ const scope = candidateMap . scope ;
69+ const quote = candidateMap . quote ;
70+ let ignoreLine = false ;
71+ if ( line ) {
72+ if (
73+ ( scope && ! ( line . start >= scope . start && line . end <= scope . end ) ) ||
74+ ( quote && ! ( line . start >= quote . start && line . end <= quote . end ) )
75+ ) {
76+ candidateMap . line = null ;
77+ }
78+ }
79+
80+ for ( const [ _ , candidate ] of Object . entries ( candidateMap ) ) {
81+ if ( ! candidate ) {
82+ continue ;
83+ }
6584 const range = this . getTrimmedRange ( text , candidate . start , candidate . end ) ;
6685 const size = range . end - range . start ;
6786 if (
0 commit comments