|
| 1 | +import _ from 'lodash' |
| 2 | +import { changeSortingOfAutoImport, getIgnoreAutoImportSetting, isAutoImportEntryShouldBeIgnored, shouldChangeSortingOfAutoImport } from '../adjustAutoImports' |
| 3 | +import { GetConfig } from '../types' |
| 4 | +import { sortBy } from 'rambda' |
| 5 | + |
| 6 | +export default (entries: ts.CompletionEntry[], languageService: ts.LanguageService, c: GetConfig) => { |
| 7 | + const ignoreAutoImportSetting = getIgnoreAutoImportSetting(c) |
| 8 | + |
| 9 | + let newEntries = entries.filter(({ sourceDisplay, name }) => { |
| 10 | + if (!sourceDisplay) return |
| 11 | + const targetModule = ts.displayPartsToString(sourceDisplay) |
| 12 | + const toIgnore = isAutoImportEntryShouldBeIgnored(ignoreAutoImportSetting, targetModule, name) |
| 13 | + return !toIgnore |
| 14 | + }) |
| 15 | + // todo I'm not sure of incomplete completion (wasnt tested) |
| 16 | + // todo don't forget to impl glob there |
| 17 | + const handledSymbolNames = new Set<string>() |
| 18 | + for (const [i, entry] of newEntries.entries()) { |
| 19 | + const { name } = entry |
| 20 | + if (!entry.sourceDisplay || handledSymbolNames.has(name)) continue |
| 21 | + if (!shouldChangeSortingOfAutoImport(name, c)) continue |
| 22 | + handledSymbolNames.add(name) |
| 23 | + const sortFn = changeSortingOfAutoImport(c, name) |
| 24 | + // TODO probably should be rewrited |
| 25 | + const entriesToSort: ts.CompletionEntry[] = [] |
| 26 | + newEntries = newEntries.filter((entry, k) => { |
| 27 | + if (k < i) return true |
| 28 | + if (entry.sourceDisplay && entry.name === name) { |
| 29 | + entriesToSort.push(entry) |
| 30 | + return false |
| 31 | + } |
| 32 | + return true |
| 33 | + }) |
| 34 | + // todo rewrite outer that for loop to index based and increment here on insert length + handledSymbolNames can be removed in that case |
| 35 | + // final one seems to be slow, e.g. it might be slowing down completions |
| 36 | + newEntries.splice(i, 0, ...sortBy(({ sourceDisplay }) => sortFn(ts.displayPartsToString(sourceDisplay)), entriesToSort)) |
| 37 | + } |
| 38 | + return newEntries |
| 39 | +} |
0 commit comments