Skip to content

Commit

Permalink
manual detection of multiline simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Mar 26, 2024
1 parent 2746308 commit ea3e21a
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/extension/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { supportedLanguages } from '../common/languages'
import {
ALL_BRACKETS,
API_PROVIDER,
CLOSING_BRACKETS,
EXTENSION_NAME,
OPENING_BRACKETS,
PROVIDER_NAMES,
QUOTES,
SKIP_DECLARATION_SYMBOLS
Expand Down Expand Up @@ -64,13 +66,43 @@ export const getIsBracket = (char: string): char is Bracket => {
return ALL_BRACKETS.includes(char as Bracket)
}

export const getIsSingleBracket = (completion: string) =>
completion.length === 1 && getIsBracket(completion)
export const getIsClosingBracket = (char: string): char is Bracket => {
return CLOSING_BRACKETS.includes(char as Bracket)
}

export const getIsOpeningBracket = (char: string): char is Bracket => {
return OPENING_BRACKETS.includes(char as Bracket)
}

export const getIsSingleBracket = (chars: string) =>
chars?.length === 1 && getIsBracket(chars)

export const getIsOnlyOpeningBrackets = (chars: string) => {
if (!chars || !chars.length) return false

for (const char of chars) {
if (!getIsOpeningBracket(char)) {
return false
}
}
return true
}

export const getIsOnlyClosingBrackets = (chars: string) => {
if (!chars || !chars.length) return false

for (const char of chars) {
if (!getIsClosingBracket(char)) {
return false
}
}
return true
}

export const getIsOnlyBrackets = (completion: string) => {
if (completion.length === 0) return false
export const getIsOnlyBrackets = (chars: string) => {
if (!chars || !chars.length) return false

for (const char of completion) {
for (const char of chars) {
if (!getIsBracket(char)) {
return false
}
Expand Down Expand Up @@ -211,7 +243,38 @@ export const isCursorInEmptyString = () => {
return QUOTES.includes(charBefore) && QUOTES.includes(charAfter)
}

export const getNextLineIsClosingBracket = () => {
const editor = window.activeTextEditor
if (!editor) return false
const position = editor.selection.active
const nextLineText = editor.document
.lineAt(Math.min(position.line + 1, editor.document.lineCount -1))
.text.trim()
return getIsOnlyClosingBrackets(nextLineText)
}

export const getPreviousLineIsOpeningBracket = () => {
const editor = window.activeTextEditor
if (!editor) return false
const position = editor.selection.active
const previousLineCharacter = editor.document
.lineAt(Math.max(position.line - 1, 0))
.text.trim()
.split('')
.reverse()[0]
return getIsOnlyOpeningBrackets(previousLineCharacter)
}

export const getIsMultiLineCompletion = () => {
const nextLineIsClosingBracket = getNextLineIsClosingBracket()
const previousLineIsOpeningBracket = getPreviousLineIsOpeningBracket()
if (
previousLineIsOpeningBracket &&
nextLineIsClosingBracket &&
!getHasLineTextBeforeAndAfter()
) {
return true
}
return !getHasLineTextBeforeAndAfter() && !isCursorInEmptyString()
}

Expand Down

0 comments on commit ea3e21a

Please sign in to comment.