mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
fix(lsp/java): on-demand imports cause definition and reference providers to fail (fixes #1449, #1211)
This commit is contained in:
@@ -191,7 +191,7 @@ public class FindHelper {
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Matcher matcher = Pattern.compile("\\b" + name + "\\b").matcher(contents);
|
||||
Matcher matcher = Pattern.compile("\\b" + Pattern.quote(name.toString()) + "\\b").matcher(contents);
|
||||
matcher.region(start, end);
|
||||
if (matcher.find()) {
|
||||
return matcher.start();
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.androidide.lsp.java.utils
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.itsaky.androidide.lsp.java.JavaLSPTest
|
||||
import com.itsaky.androidide.lsp.models.DefinitionParams
|
||||
import com.itsaky.androidide.models.Position
|
||||
import com.itsaky.androidide.progress.ICancelChecker
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
/**
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.DEFAULT_VALUE_STRING)
|
||||
class FindHelperTest {
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
JavaLSPTest.setup()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test FindHelper#findNameIn behavior with on-demand import`() {
|
||||
JavaLSPTest.apply {
|
||||
openFile("utils/FindHelperRegexElements")
|
||||
|
||||
// Find definition for 'field' class of type 'String'
|
||||
val position = Position(9, 7)
|
||||
val params = DefinitionParams(file!!, position, ICancelChecker.NOOP)
|
||||
val definitions = runBlocking { server.findDefinition(params) }
|
||||
assertThat(definitions).isNotNull()
|
||||
assertThat(definitions.locations).hasSize(1)
|
||||
assertThat(definitions.locations[0].range.contains(Position(6, 20))).isTrue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,7 +145,15 @@ constructor(
|
||||
fun compareByEnd(other: Range): Int = end.compareTo(other.end)
|
||||
|
||||
fun contains(position: Position): Boolean {
|
||||
return !(position.line < start.line || position.line > end.line)
|
||||
if (position.line < start.line || position.line > end.line) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (start.line == end.line) {
|
||||
return position.column >= start.column && position.column <= end.column
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.itsaky.androidide.lsp.java.test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private String field;
|
||||
|
||||
public static void main(String[] args) {
|
||||
field = "Hello World!";
|
||||
|
||||
System.out.println(field);
|
||||
|
||||
final var list = new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user