Skip to content

Commit

Permalink
fix: try to respect user's choice of casing when auto completing sql
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Sep 11, 2024
1 parent 1baba90 commit 451c841
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions apps/web/src/components/MonacoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ const keywordRanking = [
'DO',
]

function adjustCasing(currentWord: string, suggestion: string): string {
console.log(currentWord, suggestion)

// Check if the current word is all uppercase
if (currentWord === currentWord.toUpperCase()) {
return suggestion.toUpperCase()
}

// Check if the current word is all lowercase
if (currentWord === currentWord.toLowerCase()) {
return suggestion.toLowerCase()
}

// Check if the current word is in title case (first letter uppercase, rest lowercase)
if (
currentWord[0] === currentWord[0].toUpperCase() &&
currentWord.slice(1) === currentWord.slice(1).toLowerCase()
) {
return suggestion[0].toUpperCase() + suggestion.slice(1).toLowerCase()
}

// if last character is uppercase, make the suggestion uppercase
if (
currentWord[currentWord.length - 1] ===
currentWord[currentWord.length - 1].toUpperCase()
) {
return suggestion.toUpperCase()
}

// if last character is lowercase, make the suggestion lowercase
if (
currentWord[currentWord.length - 1] ===
currentWord[currentWord.length - 1].toLowerCase()
) {
return suggestion.toLowerCase()
}

return suggestion
}

type KnownMonacoKind =
| languages.CompletionItemKind.Keyword
| languages.CompletionItemKind.Class
Expand Down Expand Up @@ -316,9 +356,7 @@ function MonacoProvider({ children }: Props) {
anchor
)

const currentWord = model
.getWordAtPosition(position)
?.word.toLowerCase()
const currentWord = model.getWordAtPosition(position)?.word

const suggestions = (
await Promise.all(
Expand All @@ -335,7 +373,9 @@ function MonacoProvider({ children }: Props) {
const suggestion: languages.CompletionItem[] = result.options
.filter((o) => {
if (currentWord) {
return o.label.toLowerCase().startsWith(currentWord)
return o.label
.toLowerCase()
.startsWith(currentWord.toLowerCase())
}

return true
Expand All @@ -346,11 +386,16 @@ function MonacoProvider({ children }: Props) {
? getMonacoKeywordKind(c.type)
: getMonacoSchemaKind(c.type)
: languages.CompletionItemKind.Text

let insertText = currentWord
? adjustCasing(currentWord, c.label)
: c.label
insertText = insertText.slice(currentWord?.length)
return {
label: c.displayLabel ?? c.label,
kind,
// remove the intersection of the current word
insertText: c.label.slice(currentWord?.length),
insertText,
range: {
startLineNumber: position.lineNumber,
startColumn: position.column,
Expand Down

0 comments on commit 451c841

Please sign in to comment.