Skip to content

Commit 2764835

Browse files
committed
fix: allow to have space before ignore format directive and any content after
1 parent 55c468d commit 2764835

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

src/moreCompletions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ export default () => {
66
defaultJsSupersetLangsWithVue,
77
{
88
provideCompletionItems(document, position, token, context) {
9-
const regex = /\/\/@?[\w-]*/
9+
const regex = /\/\/ ?@?[\w-]*/
1010
let range = document.getWordRangeAtPosition(position, regex)
1111
if (!range) return
1212
const rangeText = document.getText(range)
1313
if (rangeText !== document.lineAt(position).text.trim()) {
1414
return
1515
}
1616

17-
range = range.with(range.start.translate(0, 2), range.end)
17+
range = range.with(range.start.translate(0, rangeText.indexOf('@')), range.end)
1818
const tsDirectives = ['@ts-format-ignore-line', '@ts-format-ignore-region', '@ts-format-ignore-endregion']
1919
return tsDirectives.map((directive, i) => {
2020
const completionItem = new vscode.CompletionItem(directive, vscode.CompletionItemKind.Snippet)
2121
completionItem.range = range
22-
completionItem.sortText = `z${i}`
22+
completionItem.sortText = `@z${i}`
2323
return completionItem
2424
})
2525
},

typescript/src/decorateFormatFeatures.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,41 @@ import { GetConfig } from './types'
22
import { patchMethod } from './utils'
33

44
export 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
}

typescript/test/completions.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ const a = {
510510
//@ts-format-ignore-region
511511
a: 1,
512512
a1: 2,
513-
//@ts-format-ignore-endregion
513+
// @ts-format-ignore-endregion
514514
b: 3,
515-
//@ts-format-ignore-line
515+
// @ts-format-ignore-line Any content don't care
516516
c: 4,
517517
}`)
518518
const edits = languageService.getFormattingEditsForRange(entrypoint, 0, files[entrypoint]!.length, ts.getDefaultFormatCodeSettings())
@@ -527,7 +527,7 @@ const a = {
527527
"newText": " ",
528528
"span": {
529529
"length": 2,
530-
"start": 108,
530+
"start": 109,
531531
},
532532
},
533533
]

0 commit comments

Comments
 (0)