Skip to content

Commit 0506e74

Browse files
committed
feat: convert to Typescript
1 parent 88c8d32 commit 0506e74

File tree

9 files changed

+583
-635
lines changed

9 files changed

+583
-635
lines changed

src/CodeFormatManager.js

Lines changed: 0 additions & 467 deletions
This file was deleted.

src/CodeFormatManager.ts

Lines changed: 392 additions & 0 deletions
Large diffs are not rendered by default.

src/config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import featureConfig from "@atom-ide-community/nuclide-commons-atom/feature-config"
2+
3+
export function getFormatOnSave(editor: atom$TextEditor): boolean {
4+
const formatOnSave = featureConfig.get("atom-ide-code-format.formatOnSave", {
5+
scope: editor.getRootScopeDescriptor(),
6+
}) as any
7+
return formatOnSave == null ? false : formatOnSave
8+
}
9+
10+
export function getFormatOnType(): boolean {
11+
return featureConfig.getWithDefaults("atom-ide-code-format.formatOnType", false)
12+
}

src/main.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/main.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { BusySignalService } from "atom-ide-base"
2+
import type {
3+
CodeFormatProvider,
4+
RangeCodeFormatProvider,
5+
FileCodeFormatProvider,
6+
OnTypeCodeFormatProvider,
7+
OnSaveCodeFormatProvider,
8+
} from "./types"
9+
10+
import CodeFormatManager from "./CodeFormatManager"
11+
12+
export { default as config } from "./config.json"
13+
14+
let codeFormatManager: CodeFormatManager
15+
16+
export function activate() {
17+
codeFormatManager = new CodeFormatManager()
18+
}
19+
20+
export function consumeLegacyProvider(provider: CodeFormatProvider): IDisposable {
21+
// Legacy providers used `selector` / `inclusionPriority`.
22+
// $FlowIgnore legacy API compatability.
23+
provider.grammarScopes =
24+
provider.grammarScopes ||
25+
// $FlowIgnore
26+
(provider.selector != null ? provider.selector.split(", ") : null)
27+
provider.priority =
28+
provider.priority != null
29+
? provider.priority
30+
: // $FlowFixMe(>=0.68.0) Flow suppress (T27187857)
31+
provider.inclusionPriority != null
32+
? provider.inclusionPriority
33+
: 0
34+
if (provider.formatCode) {
35+
return consumeRangeProvider(provider)
36+
} else if (provider.formatEntireFile) {
37+
return consumeFileProvider(provider)
38+
} else if (provider.formatAtPosition) {
39+
return consumeOnTypeProvider(provider)
40+
} else if (provider.formatOnSave) {
41+
return consumeOnSaveProvider(provider)
42+
}
43+
throw new Error("Invalid code format provider")
44+
}
45+
46+
export function consumeRangeProvider(provider: RangeCodeFormatProvider): IDisposable {
47+
return codeFormatManager.addRangeProvider(provider)
48+
}
49+
50+
export function consumeFileProvider(provider: FileCodeFormatProvider): IDisposable {
51+
return codeFormatManager.addFileProvider(provider)
52+
}
53+
54+
export function consumeOnTypeProvider(provider: OnTypeCodeFormatProvider): IDisposable {
55+
return codeFormatManager.addOnTypeProvider(provider)
56+
}
57+
58+
export function consumeOnSaveProvider(provider: OnSaveCodeFormatProvider): IDisposable {
59+
return codeFormatManager.addOnSaveProvider(provider)
60+
}
61+
62+
export function consumeBusySignal(busySignalService: BusySignalService): IDisposable {
63+
return codeFormatManager.consumeBusySignal(busySignalService)
64+
}
65+
66+
export function deactivate() {
67+
codeFormatManager.dispose()
68+
}

src/tsconfig.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"compilerOptions": {
3+
"strict": false,
4+
"strictNullChecks": false,
5+
"noUnusedLocals": false,
6+
"noUnusedParameters": false,
7+
"noImplicitReturns": false,
8+
"noImplicitAny": false,
9+
"noImplicitThis": false,
10+
"noFallthroughCasesInSwitch": false,
11+
"declaration": true,
12+
"emitDecoratorMetadata": true,
13+
"experimentalDecorators": true,
14+
"incremental": true,
15+
"inlineSourceMap": true,
16+
"inlineSources": true,
17+
"preserveSymlinks": true,
18+
"removeComments": false,
19+
"isolatedModules": true,
20+
"jsx": "preserve",
21+
"jsxImportSource": "solid-js",
22+
"downlevelIteration": true,
23+
"lib": ["ES2019", "dom"],
24+
"target": "ES2018",
25+
"allowJs": true,
26+
"esModuleInterop": true,
27+
"resolveJsonModule": true,
28+
"module": "commonjs",
29+
"moduleResolution": "node",
30+
"importHelpers": false,
31+
"outDir": "../dist"
32+
},
33+
"compileOnSave": true
34+
}

src/types.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/types.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import type { TextEdit } from "@atom-ide-community/nuclide-commons-atom/text-edit"
2+
3+
/**
4+
* A brief overview of the different code formatting providers:
5+
*
6+
* == Range formatters == These accept a range to format and return a list of edits to apply. These will always be
7+
* preferred over file formatters when a range is selected.
8+
*
9+
* == File formatters == These always return the result of formatting the entire file. To compensate, they can return a
10+
* custom cursor position to avoid disruption. These will be preferred over range formatters for whole-file formatting.
11+
*
12+
* == onType formatters == These are run after every typing event in a selected editor.
13+
*
14+
* == onSave formatters == These are run whenever a selected editor is saved. If the global format-on-save option is
15+
* enabled, then file/range formatters will be triggered on save (even if no save formatters are provided). Obviously,
16+
* save formatters are preferred in this case.
17+
*/
18+
19+
/**
20+
* Formats the range specified, and returns a list of text edits to apply. Text edits must be non-overlapping and
21+
* preferably in reverse-sorted order.
22+
*/
23+
export type RangeCodeFormatProvider = {
24+
formatCode: (editor: atom$TextEditor, range: atom$Range) => Promise<Array<TextEdit>>
25+
priority: number
26+
readonly grammarScopes?: Array<string>
27+
}
28+
29+
/**
30+
* Formats the range specified, but returns the entire file (along with the new cursor position). Useful for
31+
* less-flexible providers like clang-format.
32+
*/
33+
export type FileCodeFormatProvider = {
34+
formatEntireFile: (
35+
editor: atom$TextEditor,
36+
range: atom$Range
37+
) => Promise<
38+
| {
39+
newCursor?: number
40+
formatted: string
41+
}
42+
| undefined
43+
| null
44+
>
45+
priority: number
46+
readonly grammarScopes?: Array<string>
47+
}
48+
49+
/**
50+
* Formats around the given position, and returns a list of text edits to apply, similar to `formatCode`. The provider
51+
* determines the exact range to format based on what's at that position.
52+
*
53+
* This will automatically triggered after every typing event.
54+
*/
55+
export type OnTypeCodeFormatProvider = {
56+
formatAtPosition: (
57+
editor: atom$TextEditor,
58+
position: atom$Point,
59+
triggerCharacter: string
60+
) => Promise<Array<TextEdit>>
61+
priority: number
62+
readonly grammarScopes?: Array<string>
63+
keepCursorPosition: boolean
64+
}
65+
66+
/** Formats files after save events. */
67+
export type OnSaveCodeFormatProvider = {
68+
formatOnSave: (editor: atom$TextEditor) => Promise<Array<TextEdit>>
69+
priority: number
70+
readonly grammarScopes?: Array<string>
71+
}
72+
73+
export type CodeFormatProvider =
74+
| RangeCodeFormatProvider
75+
| FileCodeFormatProvider
76+
| OnTypeCodeFormatProvider
77+
| OnSaveCodeFormatProvider

0 commit comments

Comments
 (0)