-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Stabilise returned completions by improving deduplication + extra completions for constructors #19976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rochala
merged 10 commits into
scala:main
from
rochala:improved-deduplication-and-constructors-search
Apr 12, 2024
Merged
Stabilise returned completions by improving deduplication + extra completions for constructors #19976
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
066f15a
Stabilize deduplication of imports, add missing apply and constructor…
rochala 34f97be
Change how we add constructors, refactors
rochala 83e8ef7
Remove unused imports, fields
rochala a55824e
Apply comments, make new completions work with fully qualified path p…
rochala 407e16d
remove unused imports
rochala f89448e
Fix missing braces in interpolator, apply last review
rochala e343d36
Fix ordering and deduplication of methods coming from traits
rochala 8414dd9
Add tests that verify private members are filtered
rochala adde393
Adjust test
rochala bc7c848
Don't show extra applies for non-type-forwarder vals
rochala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
presentation-compiler/src/main/dotty/tools/pc/completions/CompletionAffix.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package dotty.tools.pc.completions | ||
|
||
import org.eclipse.lsp4j.Position | ||
import org.eclipse.lsp4j.Range | ||
|
||
/** | ||
* @param suffixes which we should insert | ||
* @param prefixes which we should insert | ||
* @param snippet which suffix should we insert the snippet $0 | ||
*/ | ||
case class CompletionAffix( | ||
suffixes: Set[Suffix], | ||
prefixes: Set[Prefix], | ||
rochala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
snippet: Suffix, | ||
currentPrefix: Option[String], | ||
): | ||
def addLabelSnippet = suffixes.exists(_.kind == SuffixKind.Bracket) | ||
def hasSnippet = snippet.kind != SuffixKind.NoSuffix | ||
def chain(copyFn: CompletionAffix => CompletionAffix) = copyFn(this) | ||
def withNewSuffix(kind: Suffix) = this.copy(suffixes = suffixes + kind) | ||
def withNewPrefix(kind: Prefix) = this.copy(prefixes = prefixes + kind) | ||
def withCurrentPrefix(currentPrefix: String) = this.copy(currentPrefix = Some(currentPrefix)) | ||
def withNewSuffixSnippet(suffix: Suffix) = | ||
this.copy(suffixes = suffixes + suffix, snippet = suffix) | ||
|
||
def nonEmpty: Boolean = suffixes.nonEmpty || prefixes.nonEmpty | ||
|
||
def toSuffix: String = | ||
def loop(suffixes: List[SuffixKind]): String = | ||
def cursor = if suffixes.head == snippet.kind then "$0" else "" | ||
suffixes match | ||
case SuffixKind.Brace :: tail => s"($cursor)" + loop(tail) | ||
case SuffixKind.Bracket :: tail => s"[$cursor]" + loop(tail) | ||
case SuffixKind.Template :: tail => s" {$cursor}" + loop(tail) | ||
case _ => "" | ||
loop(suffixes.toList.map(_.kind)) | ||
|
||
def toSuffixOpt: Option[String] = | ||
val edit = toSuffix | ||
if edit.nonEmpty then Some(edit) else None | ||
|
||
|
||
given Ordering[Position] = Ordering.by(elem => (elem.getLine, elem.getCharacter)) | ||
|
||
def toInsertRange: Option[Range] = | ||
import scala.language.unsafeNulls | ||
|
||
val ranges = prefixes.collect: | ||
case Affix(_, Some(range)) => range | ||
.toList | ||
for | ||
startPos <- ranges.map(_.getStart).minOption | ||
endPos <- ranges.map(_.getEnd).maxOption | ||
yield Range(startPos, endPos) | ||
|
||
private def loopPrefix(prefixes: List[PrefixKind]): String = | ||
prefixes match | ||
case PrefixKind.New :: tail => "new " + loopPrefix(tail) | ||
case _ => "" | ||
|
||
/** | ||
* We need to insert previous prefix, but we don't want to display it in the label i.e. | ||
* ```scala | ||
* scala.util.Tr@@ | ||
* ```` | ||
* should return `new Try[T]: Try[T]` | ||
* but insert `new scala.util.Try` | ||
* | ||
*/ | ||
def toInsertPrefix: String = | ||
loopPrefix(prefixes.toList.map(_.kind)) + currentPrefix.getOrElse("") | ||
|
||
def toPrefix: String = | ||
loopPrefix(prefixes.toList.map(_.kind)) | ||
|
||
end CompletionAffix | ||
|
||
object CompletionAffix: | ||
val empty = CompletionAffix( | ||
suffixes = Set.empty, | ||
prefixes = Set.empty, | ||
snippet = Affix(SuffixKind.NoSuffix), | ||
currentPrefix = None, | ||
) | ||
|
||
enum SuffixKind: | ||
case Brace, Bracket, Template, NoSuffix | ||
|
||
enum PrefixKind: | ||
case New | ||
|
||
type Suffix = Affix[SuffixKind] | ||
type Prefix = Affix[PrefixKind] | ||
|
||
private case class Affix[+T](kind: T, insertRange: Option[Range] = None) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.