Skip to content

[DRAFT] autoimport #1553

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,11 +1119,10 @@
// !!! CompletionInfoFlags

// import { type | -> token text should be blank
// !!! fuzzymatch
// var lowerCaseTokenText string
// if previousToken != nil && ast.IsIdentifier(previousToken) && !(previousToken == contextToken && importStatementCompletion != nil){
// lowerCaseTokenText = strings.ToLower(previousToken.Text())
// }
var lowerCaseTokenText string
if previousToken != nil && ast.IsIdentifier(previousToken) && !(previousToken == contextToken && importStatementCompletion != nil) {
lowerCaseTokenText = strings.ToLower(previousToken.Text())
}

// !!! moduleSpecifierCache := host.getModuleSpecifierCache();
// !!! packageJsonAutoImportProvider := host.getPackageJsonAutoImportProvider();
Expand Down Expand Up @@ -1162,9 +1161,7 @@
if itemData != nil {
return true
}
/// !!!
// return charactersFuzzyMatchInString(symbolName, lowerCaseTokenText);
return false
return charactersFuzzyMatchInString(symbolName, lowerCaseTokenText)
},
func(info []*SymbolExportInfo, symbolName string, isFromAmbientModule bool, exportMapKey ExportMapInfoKey) []*SymbolExportInfo {
if itemData != nil && !core.Some(info, func(i *SymbolExportInfo) bool {
Expand Down Expand Up @@ -3402,6 +3399,49 @@
}
}

// True if the first character of `lowercaseCharacters` is the first character
// of some "word" in `identiferString` (where the string is split into "words"
// by camelCase and snake_case segments), then if the remaining characters of
// `lowercaseCharacters` appear, in order, in the rest of `identifierString`.//
// True:
// 'state' in 'useState'
// 'sae' in 'useState'
// 'viable' in 'ENVIRONMENT_VARIABLE'//
// False:
// 'staet' in 'useState'
// 'tate' in 'useState'
// 'ment' in 'ENVIRONMENT_VARIABLE'
func charactersFuzzyMatchInString(identifierString string, lowercaseCharacters string) bool {
if lowercaseCharacters == "" {
return true
}

var prevChar *rune
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you might be able to get away with using 0 here, given an identifer can't contain NUL.

matchedFirstCharacter := false
characterIndex := 0
length := len(identifierString)
for strIndex := 0; strIndex < length; strIndex++ {

Check failure on line 3423 in internal/ls/completions.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

for loop can be changed to use an integer range (Go 1.22+) (intrange)
strChar := rune(identifierString[strIndex])
testChar := rune(lowercaseCharacters[strIndex])
if strChar == testChar || strChar == unicode.ToUpper(testChar) {
willMatchFirstChar := prevChar == nil || // Beginning of word
'a' <= *prevChar && *prevChar <= 'z' && 'A' <= strChar && strChar <= 'Z' || // camelCase transition
*prevChar == '_' && strChar != '_' // snake_case transition
matchedFirstCharacter = matchedFirstCharacter || willMatchFirstChar
if matchedFirstCharacter {
characterIndex++
}
if characterIndex == len(lowercaseCharacters) {
return true
}
}
prevChar = ptrTo(strChar)
}

// Did not find all characters
return false
}

var (
keywordCompletionsCache = collections.SyncMap[KeywordCompletionFilters, []*lsproto.CompletionItem]{}
allKeywordCompletions = sync.OnceValue(func() []*lsproto.CompletionItem {
Expand Down Expand Up @@ -5206,7 +5246,7 @@
}
}

completionData := l.getCompletionData(program, ch, file, position, itemData, preferences) // &UserPreferences{IncludeCompletionsForModuleExports: ptrTo(true), IncludeCompletionsWithInsertText: ptrTo(true)}
completionData := l.getCompletionData(program, ch, file, position, itemData, &UserPreferences{IncludeCompletionsForModuleExports: ptrTo(true), IncludeCompletionsWithInsertText: ptrTo(true)})
if completionData == nil {
return detailsData{}
}
Expand Down
Loading