-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lessen restarts, stabilize, and more opinionated defaults (#2)
Lessen restarts: - Won't restart if there is already workspace configuration and configured properly. - Won't write configuration if runtime target is node. This can be configured by `tsdetect.doNotCreateOnNode`. - Won't overwrite configuration if there already exists file by default. This can be configured by `tsdetect.doNothingIfConfigExists`. - Overriding more options can be set by `tsdetect.{deno,node}Override` BREAKING CHANGE: Only left workspace configuration target.
- Loading branch information
1 parent
386d6ee
commit 6393324
Showing
9 changed files
with
143 additions
and
240 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,8 +1,8 @@ | ||
function! tsdetect#coc#setup_switch(mode, config_type) abort | ||
function! tsdetect#coc#setup_switch(mode) abort | ||
augroup tsdetect#coc#setup_switch | ||
autocmd! | ||
if a:mode == 'auto' | ||
execute printf("autocmd User tsdetect#detect ++nested call tsdetect#coc#auto#switch_%s()", a:config_type) | ||
autocmd User tsdetect#detect ++nested if get(g:, 'coc_enabled', 0) | call tsdetect#coc#auto#switch() | endif | ||
endif | ||
augroup END | ||
endfunction |
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,41 +1,28 @@ | ||
let s:ephemeral_did_configured = 0 | ||
|
||
let s:ephemeral_condition = "if s:ephemeral_did_configured" | ||
let s:permanent_condition = "if get(g:, 'tsdetect#coc#auto#switched_%s_%s', 0)" | ||
|
||
for [s:config_type, s:condition] in [ | ||
\ ["ephemeral", s:ephemeral_condition], | ||
\ ["workspace", s:permanent_condition], | ||
\ ["user", s:permanent_condition], | ||
\ ] | ||
for [s:node, s:deno] in [["node", "deno"], ["deno", "node"]] | ||
execute join([ | ||
\ printf("function! tsdetect#coc#auto#switch_%s_%s() abort", s:config_type, s:node), | ||
\ printf(" doautocmd User tsdetect#coc#auto#swtich#%s#%s#before", s:config_type, s:node), | ||
\ s:config_type == 'ephemeral' ? "let s:ephemeral_did_configured = 1" : "", | ||
\ printf(" let g:tsdetect#coc#auto#switched_%s_%s = 1", s:config_type, s:node), | ||
\ printf(" let g:tsdetect#coc#auto#switched_%s_%s = 0", s:config_type, s:deno), | ||
\ printf(" CocCommand tsdetect.internal.%s.%s.initializeWorkspace", s:config_type, s:node), | ||
\ printf("endfunction"), | ||
\ printf(""), | ||
\ printf("function! tsdetect#coc#auto#switch_%s_%s_if_necessary() abort", s:config_type, s:node), | ||
\ s:config_type == 'ephemeral' ? s:ephemeral_condition : printf(s:permanent_condition, s:config_type, s:node), | ||
\ printf(" return"), | ||
\ printf(" endif"), | ||
\ printf(" call tsdetect#coc#auto#switch_%s_%s()", s:config_type, s:node), | ||
\ printf("endfunction"), | ||
\ ], "\n") | ||
endfor | ||
for [s:node, s:deno] in [["node", "deno"], ["deno", "node"]] | ||
execute join([ | ||
\ printf("function! tsdetect#coc#auto#switch_%s() abort", s:config_type), | ||
\ printf(" if !exists('b:tsdetect_is_node')"), | ||
\ printf("function! tsdetect#coc#auto#switch_%s() abort", s:node), | ||
\ printf(" doautocmd User tsdetect#coc#auto#swtich#%s#before", s:node), | ||
\ printf(" let g:tsdetect#coc#auto#switched_%s = 1", s:node), | ||
\ printf(" let g:tsdetect#coc#auto#switched_%s = 0", s:deno), | ||
\ printf(" CocCommand tsdetect.internal.%s.initializeWorkspace", s:node), | ||
\ printf("endfunction"), | ||
\ printf(""), | ||
\ printf("function! tsdetect#coc#auto#switch_%s_if_necessary() abort", s:node), | ||
\ printf(" if get(g:, 'tsdetect#coc#auto#switched_%s', 0)", s:node), | ||
\ printf(" return"), | ||
\ printf(" endif"), | ||
\ printf(" if b:tsdetect_is_node"), | ||
\ printf(" call tsdetect#coc#auto#switch_%s_node_if_necessary()", s:config_type), | ||
\ printf(" else"), | ||
\ printf(" call tsdetect#coc#auto#switch_%s_deno_if_necessary()", s:config_type), | ||
\ printf(" endif"), | ||
\ printf(" call tsdetect#coc#auto#switch_%s()", s:node), | ||
\ printf("endfunction"), | ||
\ ], "\n") | ||
endfor | ||
|
||
function! tsdetect#coc#auto#switch() abort | ||
if !exists('b:tsdetect_is_node') | ||
return | ||
endif | ||
if b:tsdetect_is_node | ||
call tsdetect#coc#auto#switch_node_if_necessary() | ||
else | ||
call tsdetect#coc#auto#switch_deno_if_necessary() | ||
endif | ||
endfunction |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum ConfigurationTarget { | ||
Global, | ||
User, | ||
Workspace, | ||
} |
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,55 +1,62 @@ | ||
import { commands, window } from "coc.nvim"; | ||
import { commands, window, workspace } from "coc.nvim"; | ||
import { ConfigurationTarget } from "./coc_internal"; | ||
import { setConfigWorkspace } from "./set_config"; | ||
import { getSettings, Settings } from "./settings"; | ||
|
||
const createTrimSameExts = (settings: Settings, target: "node" | "deno") => { | ||
return [ | ||
...settings.controlTrimSameExtsBase, | ||
...(target === "node" | ||
? settings.controlTrimSameExtsNode | ||
: settings.controlTrimSameExtsDeno), | ||
]; | ||
import { getSettings, Settings, TsRuntime } from "./settings"; | ||
|
||
const configure = async (runtime: TsRuntime, settings: Settings) => { | ||
await setConfigWorkspace("tsserver", "enable", runtime === "node"); | ||
await setConfigWorkspace("deno", "enable", runtime === "deno"); | ||
await commands.executeCommand("editor.action.restart"); | ||
const override = | ||
runtime === "node" ? settings.nodeOverride : settings.denoOverride; | ||
|
||
/* eslint-disable no-restricted-syntax,no-await-in-loop,no-continue */ | ||
for (const key of Object.keys(override)) { | ||
const ns = key.split("."); | ||
const nsKey = ns.pop(); | ||
if (typeof nsKey !== "string") { | ||
await window.showWarningMessage( | ||
`Override key '${key}' does not include any dots.`, | ||
); | ||
continue; | ||
} | ||
await setConfigWorkspace(ns.join("."), nsKey, override[key]); | ||
} | ||
/* eslint-enable no-restricted-syntax,no-await-in-loop,no-continue */ | ||
}; | ||
|
||
export const manualInitializeWorkspace = async (target: "node" | "deno") => { | ||
const settings = getSettings(); | ||
export const manualInitializeWorkspace = async (runtime: TsRuntime) => { | ||
await setConfigWorkspace("tsdetect", "mode", "manual"); | ||
const settings = getSettings(); | ||
|
||
if (target === "node") { | ||
await setConfigWorkspace("tsserver", "enable", true); | ||
await setConfigWorkspace("deno", "enable", false); | ||
} else { | ||
await commands.executeCommand("deno.initializeWorkspace"); | ||
} | ||
await configure(runtime, settings); | ||
|
||
await commands.executeCommand("editor.action.restart"); | ||
|
||
await window.showInformationMessage( | ||
`${target === "node" ? "Node" : "Deno"} workspace settings configured!`, | ||
`${runtime === "node" ? "Node" : "Deno"} workspace settings configured!`, | ||
); | ||
|
||
if (settings.controlTrimSameExts) { | ||
await setConfigWorkspace( | ||
"coc.source.file", | ||
"trimSameExts", | ||
createTrimSameExts(settings, target), | ||
); | ||
} | ||
}; | ||
|
||
export const configSwitch = async ( | ||
target: "node" | "deno", | ||
setConfig: (ns: string, key: string, value: unknown) => Promise<void>, | ||
) => { | ||
export const autoInitializeWorkspace = async (runtime: TsRuntime) => { | ||
const workspaceConfigFile = workspace.getConfigFile( | ||
ConfigurationTarget.Workspace, | ||
); | ||
const settings = getSettings(); | ||
await setConfig("tsserver", "enable", target === "node"); | ||
await setConfig("deno", "enable", target === "deno"); | ||
if (settings.controlTrimSameExts) { | ||
await setConfig( | ||
"coc.source.file", | ||
"trimSameExts", | ||
createTrimSameExts(settings, target), | ||
); | ||
} | ||
const exists = typeof workspaceConfigFile === "string"; | ||
|
||
if (settings.doNothingIfConfigExists && exists) return; | ||
if (settings.doNotCreateOnNode && runtime === "node" && !exists) return; | ||
|
||
const tsserverConfig = workspace.getConfiguration("tsserver"); | ||
const denoConfig = workspace.getConfiguration("deno"); | ||
|
||
if (exists && tsserverConfig.get("enable") && runtime === "node") return; | ||
if (exists && denoConfig.get("enable") && runtime === "deno") return; | ||
|
||
await configure(runtime, settings); | ||
await commands.executeCommand("editor.action.restart"); | ||
await workspace.nvim.command( | ||
`doautocmd User tsdetect#coc#auto#switch#${runtime}#after`, | ||
); | ||
}; |
Oops, something went wrong.