|
| 1 | +import { GetConfig } from './types' |
| 2 | +import { patchMethod } from './utils' |
| 3 | + |
| 4 | +export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig) => { |
| 5 | + // const oldGetAllRules = tsFull.formatting.getAllRules; |
| 6 | + // tsFull.formatting.getAllRules = () => { |
| 7 | + // } |
| 8 | + |
| 9 | + const isFormattingLineIgnored = (sourceFile: ts.SourceFile, position: number) => { |
| 10 | + // const sourceFile = languageService.getProgram()!.getSourceFile(fileName)! |
| 11 | + const fullText = sourceFile.getFullText() |
| 12 | + // check that lines before line are not ignored |
| 13 | + const linesBefore = fullText.slice(0, position).split('\n') |
| 14 | + if (linesBefore[linesBefore.length - 2]?.trim() === '//@ts-format-ignore-line') { |
| 15 | + return true |
| 16 | + } |
| 17 | + |
| 18 | + let isInsideIgnoredRegion = false |
| 19 | + for (const line of linesBefore) { |
| 20 | + if (line.trim() === '//@ts-format-ignore-region') { |
| 21 | + isInsideIgnoredRegion = true |
| 22 | + } |
| 23 | + if (line.trim() === '//@ts-format-ignore-endregion') { |
| 24 | + isInsideIgnoredRegion = false |
| 25 | + } |
| 26 | + } |
| 27 | + return isInsideIgnoredRegion |
| 28 | + } |
| 29 | + // proxy.getFormattingEditsAfterKeystroke = (fileName, position, key, options) => { |
| 30 | + // // if (isFormattingLineIgnored(fileName, position)) { |
| 31 | + // // return [] |
| 32 | + // // } |
| 33 | + // return languageService.getFormattingEditsAfterKeystroke(fileName, position, key, options) |
| 34 | + // } |
| 35 | + // proxy.getFormattingEditsForDocument = (fileName, options) => { |
| 36 | + // return [] |
| 37 | + // } |
| 38 | + const toPatchFormatMethods = ['formatSelection', 'formatOnOpeningCurly', 'formatOnClosingCurly', 'formatOnSemicolon', 'formatOnEnter'] |
| 39 | + for (const toPatchFormatMethod of toPatchFormatMethods) { |
| 40 | + patchMethod(tsFull.formatting, toPatchFormatMethod as any, oldFn => (...args) => { |
| 41 | + const result = oldFn(...args) |
| 42 | + const sourceFile = args.find(arg => ts.isSourceFile(arg as any)) |
| 43 | + return result.filter(({ span }) => { |
| 44 | + if (isFormattingLineIgnored(sourceFile as ts.SourceFile, span.start)) { |
| 45 | + return false |
| 46 | + } |
| 47 | + return true |
| 48 | + }) |
| 49 | + }) |
| 50 | + } |
| 51 | + // proxy.getFormattingEditsForRange = (fileName, start, end, options) => { |
| 52 | + // return languageService.getFormattingEditsForRange(fileName, start, end, options).filter(({ span }) => { |
| 53 | + // if (isFormattingLineIgnored(fileName, span.start)) { |
| 54 | + // return false |
| 55 | + // } |
| 56 | + // return true |
| 57 | + // }) |
| 58 | + // } |
| 59 | +} |
0 commit comments