Skip to content

Commit a0cb71b

Browse files
committed
fix(lsp/java): on-demand imports cause definition and reference providers to fail (fixes AndroidIDEOfficial#1449, AndroidIDEOfficial#1211)
1 parent 7613b46 commit a0cb71b

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

lsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/FindHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static int findNameIn(CompilationUnitTree root, CharSequence name, int st
191191
} catch (IOException e) {
192192
throw new RuntimeException(e);
193193
}
194-
Matcher matcher = Pattern.compile("\\b" + name + "\\b").matcher(contents);
194+
Matcher matcher = Pattern.compile("\\b" + Pattern.quote(name.toString()) + "\\b").matcher(contents);
195195
matcher.region(start, end);
196196
if (matcher.find()) {
197197
return matcher.start();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.lsp.java.utils
19+
20+
import com.google.common.truth.Truth.assertThat
21+
import com.itsaky.androidide.lsp.java.JavaLSPTest
22+
import com.itsaky.androidide.lsp.models.DefinitionParams
23+
import com.itsaky.androidide.models.Position
24+
import com.itsaky.androidide.progress.ICancelChecker
25+
import kotlinx.coroutines.runBlocking
26+
import org.junit.Before
27+
import org.junit.Test
28+
import org.junit.runner.RunWith
29+
import org.robolectric.RobolectricTestRunner
30+
import org.robolectric.annotation.Config
31+
32+
/**
33+
* @author Akash Yadav
34+
*/
35+
@RunWith(RobolectricTestRunner::class)
36+
@Config(manifest = Config.DEFAULT_VALUE_STRING)
37+
class FindHelperTest {
38+
39+
@Before
40+
fun setup() {
41+
JavaLSPTest.setup()
42+
}
43+
44+
@Test
45+
fun `test FindHelper#findNameIn behavior with on-demand import`() {
46+
JavaLSPTest.apply {
47+
openFile("utils/FindHelperRegexElements")
48+
49+
// Find definition for 'field' class of type 'String'
50+
val position = Position(9, 7)
51+
val params = DefinitionParams(file!!, position, ICancelChecker.NOOP)
52+
val definitions = runBlocking { server.findDefinition(params) }
53+
assertThat(definitions).isNotNull()
54+
assertThat(definitions.locations).hasSize(1)
55+
assertThat(definitions.locations[0].range.contains(Position(6, 20))).isTrue()
56+
}
57+
}
58+
}

shared/src/main/java/com/itsaky/androidide/models/Locations.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,15 @@ constructor(
145145
fun compareByEnd(other: Range): Int = end.compareTo(other.end)
146146

147147
fun contains(position: Position): Boolean {
148-
return !(position.line < start.line || position.line > end.line)
148+
if (position.line < start.line || position.line > end.line) {
149+
return false
150+
}
151+
152+
if (start.line == end.line) {
153+
return position.column >= start.column && position.column <= end.column
154+
}
155+
156+
return false
149157
}
150158

151159
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.itsaky.androidide.lsp.java.test;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
private String field;
8+
9+
public static void main(String[] args) {
10+
field = "Hello World!";
11+
12+
System.out.println(field);
13+
14+
final var list = new ArrayList<String>();
15+
}
16+
}

0 commit comments

Comments
 (0)