11import type tslib from 'typescript/lib/tsserverlibrary'
2+ import { SetOptional } from 'type-fest'
23
3- export function findChildContainingPosition (
4- typescript : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
5- sourceFile : ts . SourceFile ,
6- position : number ,
7- ) : ts . Node | undefined {
4+ export function findChildContainingPosition ( typescript : typeof tslib , sourceFile : ts . SourceFile , position : number ) : ts . Node | undefined {
85 function find ( node : ts . Node ) : ts . Node | undefined {
96 if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) {
107 return typescript . forEachChild ( node , find ) || node
@@ -15,25 +12,41 @@ export function findChildContainingPosition(
1512 return find ( sourceFile )
1613}
1714
18- export function findChildContainingPositionMaxDepth (
19- typescript : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
20- sourceFile : ts . SourceFile ,
21- position : number ,
22- maxDepth ?: number ,
23- ) : ts . Node | undefined {
15+ export function findChildContainingPositionMaxDepth ( sourceFile : ts . SourceFile , position : number , maxDepth ?: number ) : ts . Node | undefined {
2416 let currentDepth = 0
2517 function find ( node : ts . Node ) : ts . Node | undefined {
2618 if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) {
2719 if ( ++ currentDepth === maxDepth ) return node
28- return typescript . forEachChild ( node , find ) || node
20+ return ts . forEachChild ( node , find ) || node
2921 }
3022
3123 return
3224 }
3325 return find ( sourceFile )
3426}
3527
36- export const getIndentFromPos = ( typescript : typeof import ( 'typescript/lib/tsserverlibrary' ) , sourceFile : ts . SourceFile , position : number ) => {
28+ export function getNodePath ( sourceFile : ts . SourceFile , position : number ) : ts . Node [ ] {
29+ const nodes : ts . Node [ ] = [ ]
30+ function find ( node : ts . Node ) : ts . Node | undefined {
31+ if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) {
32+ if ( node !== sourceFile ) nodes . push ( node )
33+ return ts . forEachChild ( node , find ) || node
34+ }
35+
36+ return
37+ }
38+ find ( sourceFile )
39+ return nodes
40+ }
41+
42+ // todo not impl
43+ type MatchStringValue = keyof typeof ts . SyntaxKind | '*'
44+
45+ export const matchNodePath = ( sourceFile : ts . SourceFile , position : number , candidates : MatchStringValue [ ] [ ] ) => {
46+ const nodesPath = getNodePath ( sourceFile , position )
47+ }
48+
49+ export const getIndentFromPos = ( typescript : typeof tslib , sourceFile : ts . SourceFile , position : number ) => {
3750 const { character } = typescript . getLineAndCharacterOfPosition ( sourceFile , position )
3851 return (
3952 sourceFile
@@ -63,6 +76,25 @@ export const cleanupEntryName = ({ name }: Pick<ts.CompletionEntry, 'name'>) =>
6376 return name . replace ( / ^ ★ / , '' )
6477}
6578
79+ export const boostOrAddSuggestions = ( existingEntries : ts . CompletionEntry [ ] , topEntries : SetOptional < ts . CompletionEntry , 'sortText' > [ ] ) => {
80+ const topEntryNames = topEntries . map ( ( { name } ) => name )
81+ return [
82+ ...topEntries . map ( entry => ( { ...entry , sortText : entry . sortText ?? `07` } ) ) ,
83+ ...existingEntries . filter ( ( { name } ) => ! topEntryNames . includes ( name ) ) ,
84+ ]
85+ }
86+
87+ export const boostExistingSuggestions = ( entries : ts . CompletionEntry [ ] , predicate : ( entry : ts . CompletionEntry ) => boolean | number ) => {
88+ return [ ...entries ] . sort ( ( a , b ) => {
89+ return [ a , b ]
90+ . map ( x => {
91+ const res = predicate ( x )
92+ return res === true ? 0 : res === false ? 1 : res
93+ } )
94+ . reduce ( ( a , b ) => a - b )
95+ } )
96+ }
97+
6698// Workaround esbuild bundle modules
6799export const nodeModules = __WEB__
68100 ? null
0 commit comments