-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mapping): complete mappings refactoring
- Loading branch information
Chris
authored and
Chris
committed
May 27, 2020
1 parent
aa0d6c0
commit 12f5488
Showing
6 changed files
with
173 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,36 @@ | ||
import * as vscode from "vscode"; | ||
import * as JSON5 from "json5"; | ||
import { readFileSync } from "fs"; | ||
import { Config, Mapping } from "./configuration.interface"; | ||
import { getWorkfolderTsConfigConfiguration } from "./tsconfig.service"; | ||
import { parseMappings, replaceWorkspaceRoot } from "./mapping.service"; | ||
|
||
let cachedConfiguration = new Map<string, Config>(); | ||
export async function getConfiguration( | ||
resource: vscode.Uri | ||
): Promise<Readonly<Config>> { | ||
const workspaceFolder = vscode.workspace.getWorkspaceFolder(resource); | ||
|
||
export async function subscribeToConfigurationService(): Promise< | ||
vscode.Disposable[] | ||
> { | ||
await preFetchConfiguration(); | ||
const getConfig = (key: string) => | ||
vscode.workspace.getConfiguration(key, resource); | ||
|
||
const disposables: vscode.Disposable[] = []; | ||
for (const workfolder of vscode.workspace.workspaceFolders || []) { | ||
const pattern = new vscode.RelativePattern(workfolder, "[tj]sconfig.json"); | ||
const fileWatcher = vscode.workspace.createFileSystemWatcher(pattern); | ||
fileWatcher.onDidChange(preFetchConfiguration); | ||
disposables.push(fileWatcher); | ||
} | ||
|
||
const configurationWatcher = vscode.workspace.onDidChangeConfiguration( | ||
preFetchConfiguration | ||
); | ||
disposables.push(configurationWatcher); | ||
|
||
return disposables; | ||
} | ||
|
||
export function getConfiguration(): Readonly<Config> { | ||
const currentFile = vscode.window.activeTextEditor?.document.uri; | ||
|
||
if (!currentFile) { | ||
return getWorkspaceConfiguration(); | ||
} | ||
|
||
const workSpaceKey = vscode.workspace.getWorkspaceFolder(currentFile)?.name; | ||
|
||
if (!workSpaceKey) { | ||
return getWorkspaceConfiguration(); | ||
} | ||
|
||
return cachedConfiguration.get(workSpaceKey) || getWorkspaceConfiguration(); | ||
} | ||
|
||
async function preFetchConfiguration() { | ||
for (const workfolder of vscode.workspace.workspaceFolders || []) { | ||
const configuration = await getConfigurationForWorkfolder(workfolder); | ||
cachedConfiguration.set(workfolder.name, configuration); | ||
} | ||
} | ||
|
||
async function getConfigurationForWorkfolder( | ||
workfolder: vscode.WorkspaceFolder | ||
): Promise<Config> { | ||
const workspaceConfiguration = getWorkspaceConfiguration(); | ||
const workfolderTSConfig = await getWorkfolderTsConfigConfiguration( | ||
workfolder | ||
); | ||
return { | ||
...workspaceConfiguration, | ||
...workfolderTSConfig | ||
}; | ||
} | ||
|
||
async function getWorkfolderTsConfigConfiguration( | ||
workfolder: vscode.WorkspaceFolder | ||
): Promise<Partial<Config>> { | ||
const include = new vscode.RelativePattern(workfolder, "[tj]sconfig.json"); | ||
const exclude = new vscode.RelativePattern(workfolder, "**/node_modules/**"); | ||
|
||
const files = await vscode.workspace.findFiles(include, exclude); | ||
|
||
const mappingsFromWorkspaceConfig = files.reduce( | ||
(mappings: Mapping[], file) => { | ||
try { | ||
const parsedFile = JSON5.parse(readFileSync(file.fsPath).toString()); | ||
const newMappings = createMappingsFromWorkspaceConfig(parsedFile); | ||
return [...mappings, ...newMappings]; | ||
} catch (error) { | ||
return mappings; | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const configuration = vscode.workspace.getConfiguration("path-intellisense"); | ||
const rawMappings = configuration["mappings"]; | ||
const parsedMappings = parseMappings(rawMappings); | ||
|
||
const allMappings = [...parsedMappings, ...mappingsFromWorkspaceConfig]; | ||
const mappingsWithCorrectPath = replaceWorkspaceRoot(allMappings, workfolder); | ||
|
||
return { | ||
mappings: mappingsWithCorrectPath | ||
}; | ||
} | ||
|
||
function getWorkspaceConfiguration(): Config { | ||
const cfgExtension = vscode.workspace.getConfiguration("path-intellisense"); | ||
const cfgGeneral = vscode.workspace.getConfiguration("files"); | ||
const cfgExtension = getConfig("path-intellisense"); | ||
const cfgGeneral = getConfig("files"); | ||
const mappings = await getMappings(cfgExtension, workspaceFolder); | ||
|
||
return { | ||
autoSlash: cfgExtension["autoSlashAfterDirectory"], | ||
showHiddenFiles: cfgExtension["showHiddenFiles"], | ||
withExtension: cfgExtension["extensionOnImport"], | ||
absolutePathToWorkspace: cfgExtension["absolutePathToWorkspace"], | ||
filesExclude: cfgGeneral["exclude"], | ||
mappings: [] | ||
mappings, | ||
}; | ||
} | ||
|
||
/** | ||
* From { "lib": "libraries", "other": "otherpath" } | ||
* To [ { key: "lib", value: "libraries" }, { key: "other", value: "otherpath" } ] | ||
* @param mappings { "lib": "libraries" } | ||
*/ | ||
function parseMappings(mappings: { [key: string]: string }): Mapping[] { | ||
return Object.entries(mappings).map(([key, value]) => ({ key, value })); | ||
} | ||
|
||
/** | ||
* Replace ${workspaceRoot} with workfolder.uri.path | ||
* @param mappings | ||
* @param workfolder | ||
*/ | ||
function replaceWorkspaceRoot( | ||
mappings: Mapping[], | ||
workfolder: vscode.WorkspaceFolder | ||
): Mapping[] { | ||
return mappings.map(mapping => { | ||
return { | ||
key: mapping.key, | ||
value: mapping.value.replace("${workspaceRoot}", workfolder.uri.path) | ||
}; | ||
}); | ||
} | ||
|
||
function createMappingsFromWorkspaceConfig(tsconfig: { | ||
compilerOptions: { baseUrl: string }; | ||
}): Mapping[] { | ||
const mappings: Mapping[] = []; | ||
const baseUrl = tsconfig?.compilerOptions?.baseUrl; | ||
|
||
if (baseUrl) { | ||
mappings.push({ | ||
key: baseUrl, | ||
// value: `${workfolder.uri.path}/${baseUrl}` | ||
value: "${workspaceRoot}/" + baseUrl | ||
}); | ||
} | ||
|
||
// Todo: paths property | ||
|
||
return mappings; | ||
async function getMappings( | ||
configuration: vscode.WorkspaceConfiguration, | ||
workfolder?: vscode.WorkspaceFolder | ||
): Promise<Mapping[]> { | ||
const mappings = parseMappings(configuration["mappings"]); | ||
const tsConfigMappings = await getWorkfolderTsConfigConfiguration(workfolder); | ||
const allMappings = [...mappings, ...tsConfigMappings]; | ||
return replaceWorkspaceRoot(allMappings, workfolder); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Mapping } from "./configuration.interface"; | ||
import * as vscode from "vscode"; | ||
|
||
/** | ||
* From { "lib": "libraries", "other": "otherpath" } | ||
* To [ { key: "lib", value: "libraries" }, { key: "other", value: "otherpath" } ] | ||
* @param mappings { "lib": "libraries" } | ||
*/ | ||
export function parseMappings(mappings: { [key: string]: string }): Mapping[] { | ||
return Object.entries(mappings).map(([key, value]) => ({ key, value })); | ||
} | ||
|
||
/** | ||
* Replace ${workspaceRoot} with workfolder.uri.path | ||
* @param mappings | ||
* @param workfolder | ||
*/ | ||
export function replaceWorkspaceRoot( | ||
mappings: Mapping[], | ||
workfolder?: vscode.WorkspaceFolder | ||
): Mapping[] { | ||
const rootPath = workfolder?.uri.path; | ||
|
||
if (rootPath) { | ||
return mappings.map(({ key, value }) => ({ | ||
key, | ||
value: value.replace("${workspaceRoot}", rootPath), | ||
})); | ||
} else { | ||
return mappings.filter( | ||
({ value }) => value.indexOf("${workspaceRoot}") === -1 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as vscode from "vscode"; | ||
import * as JSON5 from "json5"; | ||
import { readFileSync } from "fs"; | ||
import { Mapping } from "./configuration.interface"; | ||
|
||
export const getWorkfolderTsConfigConfiguration = memoize(async function ( | ||
workfolder: vscode.WorkspaceFolder | ||
): Promise<Mapping[]> { | ||
const include = new vscode.RelativePattern(workfolder, "[tj]sconfig.json"); | ||
const exclude = new vscode.RelativePattern(workfolder, "**/node_modules/**"); | ||
const files = await vscode.workspace.findFiles(include, exclude); | ||
|
||
return files.reduce((mappings: Mapping[], file) => { | ||
try { | ||
const parsedFile = JSON5.parse(readFileSync(file.fsPath).toString()); | ||
const newMappings = createMappingsFromWorkspaceConfig(parsedFile); | ||
return [...mappings, ...newMappings]; | ||
} catch (error) { | ||
return mappings; | ||
} | ||
}, []); | ||
}); | ||
|
||
export function subscribeToTsConfigChanges(): vscode.Disposable[] { | ||
const disposables: vscode.Disposable[] = []; | ||
for (const workfolder of vscode.workspace.workspaceFolders || []) { | ||
const pattern = new vscode.RelativePattern(workfolder, "[tj]sconfig.json"); | ||
const fileWatcher = vscode.workspace.createFileSystemWatcher(pattern); | ||
fileWatcher.onDidChange(() => invalidateCache(workfolder)); | ||
disposables.push(fileWatcher); | ||
} | ||
return disposables; | ||
} | ||
|
||
function createMappingsFromWorkspaceConfig(tsconfig: { | ||
compilerOptions: { baseUrl: string }; | ||
}): Mapping[] { | ||
const mappings: Mapping[] = []; | ||
const baseUrl = tsconfig?.compilerOptions?.baseUrl; | ||
|
||
if (baseUrl) { | ||
mappings.push({ | ||
key: baseUrl, | ||
// value: `${workfolder.uri.path}/${baseUrl}` | ||
value: "${workspaceRoot}/" + baseUrl, | ||
}); | ||
} | ||
|
||
// Todo: paths property | ||
|
||
return mappings; | ||
} | ||
|
||
/** Caching */ | ||
|
||
let cachedMappings = new Map<string, Mapping[]>(); | ||
|
||
function memoize( | ||
fn: (workfolder: vscode.WorkspaceFolder) => Promise<Mapping[]> | ||
) { | ||
async function cachedFunction( | ||
workfolder?: vscode.WorkspaceFolder | ||
): Promise<Mapping[]> { | ||
if (!workfolder) { | ||
return Promise.resolve([]); | ||
} | ||
|
||
const key = workfolder.name; | ||
const cachedMapping = cachedMappings.get(key); | ||
|
||
if (cachedMapping) { | ||
return cachedMapping; | ||
} else { | ||
let result = await fn(workfolder); | ||
cachedMappings.set(key, result); | ||
return result; | ||
} | ||
} | ||
|
||
return cachedFunction; | ||
} | ||
|
||
function invalidateCache(workfolder: vscode.WorkspaceFolder) { | ||
cachedMappings.delete(workfolder.name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.