@@ -179,26 +179,46 @@ private class MethodUse extends Use instanceof NameRef {
179179 override string getUseType ( ) { result = "method" }
180180}
181181
182+ // We don't have entities for the operator symbols, so we approximate a location.
183+ // The location spans are chosen so that they align with rust-analyzer's jump-to-def
184+ // behavior in VS Code, which means using weird locations where the end column is
185+ // before the start column in the case of unary prefix operations.
186+ private predicate operatorHasLocationInfo (
187+ Operation o , string filepath , int startline , int startcolumn , int endline , int endcolumn
188+ ) {
189+ o =
190+ // `-x`; placing the cursor before `-` jumps to `neg`, placing it after jumps to `x`
191+ any ( PrefixExpr pe |
192+ pe .getLocation ( ) .hasLocationInfo ( filepath , startline , startcolumn , _, _) and
193+ endline = startline and
194+ endcolumn = startcolumn - 1
195+ )
196+ or
197+ o =
198+ // `x + y`: placing the cursor before or after `+` jumps to `add`
199+ // `x+ y`: placing the cursor before `+` jumps to `x`, after `+` jumps to `add`
200+ any ( BinaryExpr be |
201+ be .getLhs ( ) .getLocation ( ) .hasLocationInfo ( filepath , _, _, startline , startcolumn - 2 ) and
202+ be .getRhs ( ) .getLocation ( ) .hasLocationInfo ( filepath , endline , endcolumn + 2 , _, _) and
203+ (
204+ startline < endline
205+ or
206+ endcolumn = startcolumn
207+ )
208+ )
209+ }
210+
182211private class OperationUse extends Use instanceof Operation {
212+ OperationUse ( ) { operatorHasLocationInfo ( this , _, _, _, _, _) }
213+
183214 override Definition getDefinition ( ) { result .asItemNode ( ) = this .( Call ) .getStaticTarget ( ) }
184215
185216 override string getUseType ( ) { result = "method" }
186217
187218 override predicate hasLocationInfo (
188219 string filepath , int startline , int startcolumn , int endline , int endcolumn
189220 ) {
190- // We don't have entities for the operator symbols, so approximate a location
191- this =
192- any ( PrefixExpr pe |
193- pe .getLocation ( ) .hasLocationInfo ( filepath , startline , startcolumn , _, _) and
194- pe .getExpr ( ) .getLocation ( ) .hasLocationInfo ( _, endline , endcolumn + 2 , _, _)
195- )
196- or
197- this =
198- any ( BinaryExpr be |
199- be .getLhs ( ) .getLocation ( ) .hasLocationInfo ( filepath , _, _, startline , startcolumn - 2 ) and
200- be .getRhs ( ) .getLocation ( ) .hasLocationInfo ( filepath , endline , endcolumn + 2 , _, _)
201- )
221+ operatorHasLocationInfo ( this , filepath , startline , startcolumn , endline , endcolumn )
202222 }
203223}
204224
@@ -207,10 +227,13 @@ private class IndexExprUse extends Use instanceof IndexExpr {
207227
208228 override string getUseType ( ) { result = "method" }
209229
230+ // We don't have entities for the bracket symbols, so approximate a location
231+ // The location spans are chosen so that they align with rust-analyzer's jump-to-def
232+ // behavior in VS Code.
210233 override predicate hasLocationInfo (
211234 string filepath , int startline , int startcolumn , int endline , int endcolumn
212235 ) {
213- // We don't have entities for the brackets, so approximate a location
236+ // `x[y]`: placing the cursor after `]` jumps to `index`
214237 super .getIndex ( ) .getLocation ( ) .hasLocationInfo ( filepath , _, _, startline , startcolumn - 2 ) and
215238 this .getLocation ( ) .hasLocationInfo ( _, _, _, endline , endcolumn )
216239 }
0 commit comments