Skip to content

Commit

Permalink
Pull allowed extensions from grammar definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
lierdakil committed Mar 27, 2021
1 parent efccc74 commit dc89287
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main.js.map

Large diffs are not rendered by default.

41 changes: 33 additions & 8 deletions lib/main/atom/utils/atom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Atom from "atom"
import {memoize, throttle} from "lodash"
import * as path from "path"
import {FileLocationQuery, Location, pointToLocation} from "./ts"

Expand All @@ -17,9 +18,10 @@ export function isTypescriptFile(filePath: string | undefined): boolean {
}

export function typeScriptScopes(): ReadonlyArray<string> {
const tsScopes = atom.config.get("atom-typescript").tsSyntaxScopes
if (atom.config.get("atom-typescript").allowJS) {
tsScopes.push(...atom.config.get("atom-typescript").jsSyntaxScopes)
const config = atom.config.get("atom-typescript")
const tsScopes = config.tsSyntaxScopes
if (config.allowJS) {
tsScopes.push(...config.jsSyntaxScopes)
}
return tsScopes
}
Expand All @@ -33,13 +35,36 @@ export function isTypescriptGrammar(editor: Atom.TextEditor): boolean {
return typeScriptScopes().includes(scopeName)
}

function isAllowedExtension(ext: string) {
const tsExts = atom.config.get("atom-typescript").tsFileExtensions
if (atom.config.get("atom-typescript").allowJS) {
tsExts.push(...atom.config.get("atom-typescript").jsFileExtensions)
function notNullary<T>(x: T | undefined | null): x is T {
return x != null
}

function memoizeThrottle<T, U>(func: (arg: T) => U, wait: number): (arg: T) => U {
const mem = memoize((_param: T) => throttle(func, wait, {leading: true}))
return (param: T) => mem(param)(param)! // NOTE: leading MUST be true for this ! to hold
}

const isAllowedExtension = memoizeThrottle((ext: string) => {
const config = atom.config.get("atom-typescript")
const tsExts = config.tsFileExtensions
if (config.allowJS) {
tsExts.push(...config.jsFileExtensions)
}
if (config.extensionsFromGrammars) {
const custom = atom.config.get("core.customFileTypes") ?? {}
const scopes = typeScriptScopes()
tsExts.push(
...([] as Array<string | undefined>)
.concat(
...scopes.map((scope) => atom.grammars.grammarForScopeName(scope)?.fileTypes),
...scopes.map((scope) => custom[scope]),
)
.filter(notNullary)
.map((s) => `.${s}`),
)
}
return tsExts.includes(ext)
}
}, 5000)

export function getFilePathPosition(
editor: Atom.TextEditor,
Expand Down
Empty file added lib/main/atom/utils/test.testjs
Empty file.
2 changes: 2 additions & 0 deletions lib/typings/atom-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare module "atom" {
"atom-typescript.suppressAllDiagnostics": boolean
"atom-typescript.tsFileExtensions": string[]
"atom-typescript.jsFileExtensions": string[]
"atom-typescript.extensionsFromGrammars": boolean
"atom-typescript.tsSyntaxScopes": string[]
"atom-typescript.jsSyntaxScopes": string[]
"atom-typescript.allowJS": boolean
Expand Down Expand Up @@ -77,6 +78,7 @@ declare module "atom" {
suppressAllDiagnostics: boolean
tsFileExtensions: string[]
jsFileExtensions: string[]
extensionsFromGrammars: boolean
tsSyntaxScopes: string[]
jsSyntaxScopes: string[]
allowJS: boolean
Expand Down
3 changes: 3 additions & 0 deletions lib/typings/atom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ declare module "atom" {
interface TextEditorElement {
setUpdatedSynchronously(val: boolean): void
}
interface Grammar {
fileTypes: string[]
}
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@
],
"order": 140
},
"extensionsFromGrammars": {
"title": "Determine file extensions from Atom grammar settings",
"description": "Pull recognized file extensions from grammar definitions matching the syntax scopes defined below",
"type": "boolean",
"default": true,
"order": 145
},
"tsSyntaxScopes": {
"title": "TypeScript syntax scopes",
"description": "Comma-separated list of TypeScript syntax scopes; may require Atom restart to take effect; DO NOT EDIT unless you know what you are doing",
Expand Down

0 comments on commit dc89287

Please sign in to comment.