diff --git a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/FindHelper.java b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/FindHelper.java
index 9323d3ade0..3e7a2a066f 100644
--- a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/FindHelper.java
+++ b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/FindHelper.java
@@ -191,7 +191,7 @@ public static int findNameIn(CompilationUnitTree root, CharSequence name, int st
} 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();
diff --git a/lsp/java/src/test/java/com/itsaky/androidide/lsp/java/utils/FindHelperTest.kt b/lsp/java/src/test/java/com/itsaky/androidide/lsp/java/utils/FindHelperTest.kt
new file mode 100644
index 0000000000..33a0a5db36
--- /dev/null
+++ b/lsp/java/src/test/java/com/itsaky/androidide/lsp/java/utils/FindHelperTest.kt
@@ -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 .
+ */
+
+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()
+ }
+ }
+}
\ No newline at end of file
diff --git a/shared/src/main/java/com/itsaky/androidide/models/Locations.kt b/shared/src/main/java/com/itsaky/androidide/models/Locations.kt
index 6488326718..322b6b8e32 100644
--- a/shared/src/main/java/com/itsaky/androidide/models/Locations.kt
+++ b/shared/src/main/java/com/itsaky/androidide/models/Locations.kt
@@ -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
}
/**
diff --git a/testing/resources/test-project/app/src/main/resources/utils/FindHelperRegexElements_template.java b/testing/resources/test-project/app/src/main/resources/utils/FindHelperRegexElements_template.java
new file mode 100644
index 0000000000..492f144266
--- /dev/null
+++ b/testing/resources/test-project/app/src/main/resources/utils/FindHelperRegexElements_template.java
@@ -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();
+ }
+}
\ No newline at end of file