Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix(lsp/java): on-demand imports cause definition and reference provi…
Browse files Browse the repository at this point in the history
…ders to fail (fixes #1449, #1211)
  • Loading branch information
itsaky committed Nov 13, 2023
1 parent 7613b46 commit a0cb71b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
10 changes: 9 additions & 1 deletion shared/src/main/java/com/itsaky/androidide/models/Locations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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>();
}
}

0 comments on commit a0cb71b

Please sign in to comment.