@@ -2,43 +2,41 @@ import { GetConfig } from './types'
22import { patchMethod } from './utils'
33
44export default ( proxy : ts . LanguageService , languageService : ts . LanguageService , c : GetConfig ) => {
5- // const oldGetAllRules = tsFull.formatting.getAllRules;
6- // tsFull.formatting.getAllRules = () => {
7- // }
5+ // todo: add our formatting rules!
6+ // tsFull.formatting.getAllRules
87
8+ const isExpectedDirective = ( line : string | undefined , expected : string ) => {
9+ if ( ! line ) return false
10+ line = line . trim ( )
11+ if ( ! line . startsWith ( '//' ) ) return false
12+ line = line . slice ( 2 ) . trim ( )
13+ if ( line . startsWith ( expected ) ) return true
14+ return false
15+ }
916 const isFormattingLineIgnored = ( sourceFile : ts . SourceFile , position : number ) => {
10- // const sourceFile = languageService.getProgram()!.getSourceFile(fileName)!
1117 const fullText = sourceFile . getFullText ( )
1218 // check that lines before line are not ignored
1319 const linesBefore = fullText . slice ( 0 , position ) . split ( '\n' )
14- if ( linesBefore [ linesBefore . length - 2 ] ?. trim ( ) === '// @ts-format-ignore-line') {
20+ if ( isExpectedDirective ( linesBefore [ linesBefore . length - 2 ] , ' @ts-format-ignore-line') ) {
1521 return true
1622 }
1723
1824 let isInsideIgnoredRegion = false
1925 for ( const line of linesBefore ) {
20- if ( line . trim ( ) === '// @ts-format-ignore-region') {
26+ if ( isExpectedDirective ( line , ' @ts-format-ignore-region') ) {
2127 isInsideIgnoredRegion = true
2228 }
23- if ( line . trim ( ) === '// @ts-format-ignore-endregion') {
29+ if ( isExpectedDirective ( line , ' @ts-format-ignore-endregion') ) {
2430 isInsideIgnoredRegion = false
2531 }
2632 }
2733 return isInsideIgnoredRegion
2834 }
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- // }
3835 const toPatchFormatMethods = [ 'formatSelection' , 'formatOnOpeningCurly' , 'formatOnClosingCurly' , 'formatOnSemicolon' , 'formatOnEnter' ]
3936 for ( const toPatchFormatMethod of toPatchFormatMethods ) {
4037 patchMethod ( tsFull . formatting , toPatchFormatMethod as any , oldFn => ( ...args ) => {
4138 const result = oldFn ( ...args )
39+ // arg position depends on the method, so we need to find it
4240 const sourceFile = args . find ( arg => ts . isSourceFile ( arg as any ) )
4341 return result . filter ( ( { span } ) => {
4442 if ( isFormattingLineIgnored ( sourceFile as ts . SourceFile , span . start ) ) {
@@ -48,12 +46,6 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
4846 } )
4947 } )
5048 }
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- // }
49+ // we could easily patch languageService methods getFormattingEditsForDocument, getFormattingEditsAfterKeystroke and getFormattingEditsForRange
50+ // but since formatting happens in syntax server, we don't have access to actual sourceFile, so we can't provide implementation
5951}
0 commit comments