@@ -79,7 +79,7 @@ class PcDefinitionProvider(
79
79
.untypedPath(pos.span)
80
80
.collect { case t : untpd.Tree => t }
81
81
82
- definitionsForSymbol (untpdPath.headOption.map(_.symbol).toList, uri, pos)
82
+ definitionsForSymbols (untpdPath.headOption.map(_.symbol).toList, uri, pos)
83
83
end fallbackToUntyped
84
84
85
85
private def findDefinitions (
@@ -89,7 +89,7 @@ class PcDefinitionProvider(
89
89
uri : URI ,
90
90
): DefinitionResult =
91
91
import indexed .ctx
92
- definitionsForSymbol (
92
+ definitionsForSymbols (
93
93
MetalsInteractive .enclosingSymbols(path, pos, indexed),
94
94
uri,
95
95
pos
@@ -113,68 +113,58 @@ class PcDefinitionProvider(
113
113
case Nil =>
114
114
path.headOption match
115
115
case Some (value : Literal ) =>
116
- definitionsForSymbol (List (value.typeOpt.widen.typeSymbol), uri, pos)
116
+ definitionsForSymbols (List (value.typeOpt.widen.typeSymbol), uri, pos)
117
117
case _ => DefinitionResultImpl .empty
118
118
case _ =>
119
- definitionsForSymbol(typeSymbols, uri, pos)
120
-
119
+ definitionsForSymbols(typeSymbols, uri, pos)
121
120
end findTypeDefinitions
122
121
123
- private def definitionsForSymbol (
122
+ private def definitionsForSymbols (
124
123
symbols : List [Symbol ],
125
124
uri : URI ,
126
125
pos : SourcePosition
127
126
)(using ctx : Context ): DefinitionResult =
128
- symbols match
129
- case symbols @ (sym :: other) =>
130
- val isLocal = sym.source == pos.source
131
- if isLocal then
132
- val include = Include .definitions | Include .local
133
- val (exportedDefs, otherDefs) =
134
- Interactive .findTreesMatching(driver.openedTrees(uri), include, sym)
135
- .partition(_.tree.symbol.is(Exported ))
136
-
137
- otherDefs.headOption.orElse(exportedDefs.headOption) match
138
- case Some (srcTree) =>
139
- val pos = srcTree.namePos
140
- if pos.exists then
141
- val loc = new Location (params.uri().toString(), pos.toLsp)
142
- DefinitionResultImpl (
143
- SemanticdbSymbols .symbolName(sym),
144
- List (loc).asJava,
145
- )
146
- else DefinitionResultImpl .empty
147
- case None =>
148
- DefinitionResultImpl .empty
149
- else
150
- val res = new ArrayList [Location ]()
151
- semanticSymbolsSorted(symbols)
152
- .foreach { sym =>
153
- res.addAll(search.definition(sym, params.uri()))
154
- }
155
- DefinitionResultImpl (
156
- SemanticdbSymbols .symbolName(sym),
157
- res
158
- )
159
- end if
127
+ semanticSymbolsSorted(symbols) match
160
128
case Nil => DefinitionResultImpl .empty
161
- end match
162
- end definitionsForSymbol
129
+ case syms @ ((_, headSym) :: tail) =>
130
+ val locations = syms.flatMap:
131
+ case (sym, semanticdbSymbol) =>
132
+ locationsForSymbol(sym, semanticdbSymbol, uri, pos)
133
+ DefinitionResultImpl (headSym, locations.asJava)
134
+
135
+ private def locationsForSymbol (
136
+ symbol : Symbol ,
137
+ semanticdbSymbol : String ,
138
+ uri : URI ,
139
+ pos : SourcePosition
140
+ )(using ctx : Context ): List [Location ] =
141
+ val isLocal = symbol.source == pos.source
142
+ if isLocal then
143
+ val trees = driver.openedTrees(uri)
144
+ val include = Include .definitions | Include .local
145
+ val (exportedDefs, otherDefs) =
146
+ Interactive .findTreesMatching(trees, include, symbol)
147
+ .partition(_.tree.symbol.is(Exported ))
148
+ otherDefs.headOption.orElse(exportedDefs.headOption).collect:
149
+ case srcTree if srcTree.namePos.exists =>
150
+ new Location (params.uri().toString(), srcTree.namePos.toLsp)
151
+ .toList
152
+ else search.definition(semanticdbSymbol, uri).asScala.toList
163
153
164
154
def semanticSymbolsSorted (
165
155
syms : List [Symbol ]
166
- )(using ctx : Context ): List [String ] =
156
+ )(using ctx : Context ): List [( Symbol , String ) ] =
167
157
syms
168
- .map { sym =>
158
+ .collect { case sym if sym.exists =>
169
159
// in case of having the same type and teerm symbol
170
160
// term comes first
171
161
// used only for ordering symbols that come from `Import`
172
162
val termFlag =
173
163
if sym.is(ModuleClass ) then sym.sourceModule.isTerm
174
164
else sym.isTerm
175
- (termFlag, SemanticdbSymbols .symbolName(sym))
165
+ (termFlag, sym.sourceSymbol, SemanticdbSymbols .symbolName(sym))
176
166
}
177
- .sorted
178
- .map(_._2 )
167
+ .sortBy { case (termFlag, _, name) => (termFlag, name) }
168
+ .map(_.tail )
179
169
180
170
end PcDefinitionProvider
0 commit comments