Skip to content

Instantly share code, notes, and snippets.

@olafurpg
Created January 17, 2020 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olafurpg/d15833f9b519ac286e36a5daad8f07a5 to your computer and use it in GitHub Desktop.
Save olafurpg/d15833f9b519ac286e36a5daad8f07a5 to your computer and use it in GitHub Desktop.
diff --git a/mtags/src/main/scala/scala/meta/internal/pc/AutoImports.scala b/mtags/src/main/scala/scala/meta/internal/pc/AutoImports.scala
index 34c7258..11f34b6 100644
--- a/mtags/src/main/scala/scala/meta/internal/pc/AutoImports.scala
+++ b/mtags/src/main/scala/scala/meta/internal/pc/AutoImports.scala
@@ -37,14 +37,14 @@ trait AutoImports { this: MetalsGlobal =>
}
}
+ def isImportPosition: Boolean =
+ findLastVisitedParentTree(pos).exists(_.isInstanceOf[Import])
+
def autoImportPosition(
pos: Position,
text: String
): Option[AutoImportPosition] = {
- if (lastVisistedParentTrees.isEmpty) {
- locateTree(pos)
- }
- lastVisistedParentTrees.headOption match {
+ findLastVisitedParentTree(pos) match {
case Some(_: Import) => None
case _ =>
val enclosingPackage = lastVisistedParentTrees.collectFirst {
diff --git a/mtags/src/main/scala/scala/meta/internal/pc/Completions.scala b/mtags/src/main/scala/scala/meta/internal/pc/Completions.scala
index 2361c66..0f9a807 100644
--- a/mtags/src/main/scala/scala/meta/internal/pc/Completions.scala
+++ b/mtags/src/main/scala/scala/meta/internal/pc/Completions.scala
@@ -336,6 +336,12 @@ trait Completions { this: MetalsGlobal =>
// variable but it avoids repeating traversals from the compiler
// implementation of `completionsAt(pos)`.
var lastVisistedParentTrees: List[Tree] = Nil
+ def findLastVisitedParentTree(pos: Position): Option[Tree] = {
+ if (lastVisistedParentTrees.isEmpty) {
+ locateTree(pos)
+ }
+ lastVisistedParentTrees.headOption
+ }
abstract class CompletionPosition {
def isType: Boolean = false
def isNew: Boolean = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment