Skip to content

Commit

Permalink
fix: don't skip this when insert text for method completions is ena…
Browse files Browse the repository at this point in the history
…bled

fixes #148
test: add first tests for insert text mode!
  • Loading branch information
zardoy committed Jul 11, 2023
1 parent d6d1ff4 commit 35de441
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion typescript/src/completions/functionCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (entries: ts.CompletionEntry[]) => {
if (!methodSnippet || resolveData.isAmbiguous) return
return {
...entry,
insertText: insertTextAfterEntry(entry, `(${methodSnippet.map((x, i) => `$\{${i + 1}:${x}}`).join(', ')})`),
insertText: insertTextAfterEntry(entry.insertText ?? entry.name, `(${methodSnippet.map((x, i) => `$\{${i + 1}:${x}}`).join(', ')})`),
labelDetails: {
detail: `(${methodSnippet.join(', ')})`,
description: ts.displayPartsToString(entry.sourceDisplay),
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/completions/objectLiteralCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default (prior: ts.CompletionInfo): ts.CompletionEntry[] | void => {
const insertSnippetVariant = completingStyleMap.find(([, detector]) => detector(type!, typeChecker))?.[0] ?? fallbackSnippet
if (!insertSnippetVariant) continue
const [insertSnippetText, insertSnippetPreview] = typeof insertSnippetVariant === 'function' ? insertSnippetVariant() : insertSnippetVariant
let insertText = insertTextAfterEntry(entry, insertSnippetText)
let insertText = insertTextAfterEntry(entry.name, insertSnippetText)
if (node.getSourceFile().getFullText()[position] === ',') insertText = insertText.slice(0, -1)
const index = entries.indexOf(entry)
entries.splice(index + (keepOriginal === 'before' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, {
Expand Down
1 change: 0 additions & 1 deletion typescript/src/completionsAtPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _ from 'lodash'
import { compact } from '@zardoy/utils'
import escapeStringRegexp from 'escape-string-regexp'
import inKeywordCompletions from './completions/inKeywordCompletions'
// import * as emmet from '@vscode/emmet-helper'
import isInBannedPosition from './completions/isInBannedPosition'
import { GetConfig } from './types'
import { findChildContainingExactPosition, findChildContainingPosition, isTs5, patchMethod } from './utils'
Expand Down
3 changes: 1 addition & 2 deletions typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ export const patchMethod = <T, K extends keyof T>(obj: T, method: K, overriden:
}
}

export const insertTextAfterEntry = (entryOrName: ts.CompletionEntry | string, appendText: string) =>
(typeof entryOrName === 'string' ? entryOrName : entryOrName.name).replace(/\$/g, '\\$') + appendText
export const insertTextAfterEntry = (entryOrName: string, appendText: string) => entryOrName.replace(/\$/g, '\\$') + appendText

export const matchParents: MatchParentsType = (node, treeToCompare) => {
let first = true
Expand Down
26 changes: 25 additions & 1 deletion typescript/test/completions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isGoodPositionMethodCompletion } from '../src/completions/isGoodPositio
import { findChildContainingExactPosition, isTs5 } from '../src/utils'
import handleCommand from '../src/specialCommands/handle'
import constructMethodSnippet from '../src/constructMethodSnippet'
import { defaultConfigFunc, entrypoint, settingsOverride, sharedLanguageService } from './shared'
import { currentTestingContext, defaultConfigFunc, entrypoint, settingsOverride, sharedLanguageService } from './shared'
import { fileContentsSpecialPositions, fourslashLikeTester, getCompletionsAtPosition, overrideSettings } from './testing'

const { languageService, languageServiceHost, updateProject, getCurrentFile } = sharedLanguageService
Expand Down Expand Up @@ -124,6 +124,12 @@ const compareMethodSnippetAgainstMarker = (inputMarkers: number[], marker: numbe
expect(Array.isArray(expected) ? methodSnippet : snippetToInsert, `At marker ${marker}`).toEqual(expected)
}

const assertCompletionInsertText = (marker: number, entryName: string | undefined, insertTextExpected: string) => {
const { entries } = getCompletionsAtPosition(currentTestingContext.markers[marker]!)!
const entry = entryName === undefined ? entries[0] : entries.find(({ name }) => name === entryName)
expect(entry?.insertText).toEqual(insertTextExpected)
}

describe('Method snippets', () => {
test('Misc', () => {
const [, _, markers] = fileContentsSpecialPositions(/* ts */ `
Expand Down Expand Up @@ -286,6 +292,24 @@ describe('Method snippets', () => {
compareMethodSnippetAgainstMarker(markers, 1, 'ambiguous')
compareMethodSnippetAgainstMarker(markers, 2, 'ambiguous')
})

test('methodSnippetsInsertText all', () => {
overrideSettings({
methodSnippetsInsertText: 'all',
})
fileContentsSpecialPositions(/* ts */ `
const a = (a, b) => {}
a/*1*/
class A {
test() {
test/*2*/
}
}
`)
assertCompletionInsertText(1, 'a', 'a(${1:a}, ${2:b})')
assertCompletionInsertText(2, 'test', 'this.test()')
})
})

test('Emmet completion', () => {
Expand Down
4 changes: 4 additions & 0 deletions typescript/test/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export const settingsOverride: Partial<Configuration> = {
tupleHelpSignature: true,
}
export const defaultConfigFunc = await getDefaultConfigFunc(settingsOverride)

export const currentTestingContext = {
markers: [] as number[],
}
7 changes: 5 additions & 2 deletions typescript/test/testing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash'
import { getCompletionsAtPosition as getCompletionsAtPositionRaw } from '../src/completionsAtPosition'
import { Configuration } from '../src/types'
import { defaultConfigFunc, entrypoint, sharedLanguageService, settingsOverride } from './shared'
import { defaultConfigFunc, entrypoint, sharedLanguageService, settingsOverride, currentTestingContext } from './shared'

interface CompletionPartMatcher {
names?: string[]
Expand Down Expand Up @@ -29,7 +29,9 @@ export const getCompletionsAtPosition = (pos: number, { fileName = entrypoint, s
const result = getCompletionsAtPositionRaw(
fileName,
pos,
{},
{
includeCompletionsWithInsertText: true,
},
defaultConfigFunc,
languageService,
languageServiceHost.getScriptSnapshot(entrypoint)!,
Expand Down Expand Up @@ -163,5 +165,6 @@ export const fileContentsSpecialPositions = (contents: string, fileName = entryp
if (process.env.CI) throw new Error('Only positions not allowed on CI')
return cursorPositionsOnly
}
currentTestingContext.markers = cursorPositions[2]
return cursorPositions
}

0 comments on commit 35de441

Please sign in to comment.