Skip to content

Commit

Permalink
feat: lessen restarts, stabilize, and more opinionated defaults (#2)
Browse files Browse the repository at this point in the history
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
LumaKernel authored Mar 4, 2021
1 parent 386d6ee commit 6393324
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 240 deletions.
4 changes: 2 additions & 2 deletions autoload/tsdetect/coc.vim
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
57 changes: 22 additions & 35 deletions autoload/tsdetect/coc/auto.vim
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
134 changes: 34 additions & 100 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
"name": "coc-tsdetect",
"version": "2.1.0",
"description": "coc.nvim extensions for detecting node and deno environment.",
"keywords": [
"deno",
"typescript",
"javascript",
"neovim",
"vim",
"coc.nvim"
],
"keywords": ["deno", "typescript", "javascript", "neovim", "vim", "coc.nvim"],
"license": "MIT",
"author": "Luma",
"main": "./out/coc.js",
Expand All @@ -28,12 +21,8 @@
"onLanguage:javascriptreact",
"onCommand:tsdetect.manual.deno.initializeWorkspace",
"onCommand:tsdetect.manual.node.initializeWorkspace",
"onCommand:tsdetect.internal.ephemeral.deno.initializeWorkspace",
"onCommand:tsdetect.internal.ephemeral.node.initializeWorkspace",
"onCommand:tsdetect.internal.workspace.deno.initializeWorkspace",
"onCommand:tsdetect.internal.workspace.node.initializeWorkspace",
"onCommand:tsdetect.internal.user.deno.initializeWorkspace",
"onCommand:tsdetect.internal.user.node.initializeWorkspace"
"onCommand:tsdetect.internal.deno.initializeWorkspace",
"onCommand:tsdetect.internal.node.initializeWorkspace"
],
"contributes": {
"commands": [
Expand All @@ -48,32 +37,12 @@
"category": "TypeScript Language Server"
},
{
"command": "tsdetect.internal.ephemeral.deno.initializeWorkspace",
"title": "Initialize Deno Silently Without Saving Config",
"category": "Deno Language Server"
},
{
"command": "tsdetect.internal.ephemeral.node.initializeWorkspace",
"title": "Initialize Node Silently Without Saving Config",
"category": "TypeScript Language Server"
},
{
"command": "tsdetect.internal.user.deno.initializeWorkspace",
"title": "Initialize Deno Silently In User Config",
"category": "Deno Language Server"
},
{
"command": "tsdetect.internal.user.node.initializeWorkspace",
"title": "Initialize Node Silently In User Config",
"category": "TypeScript Language Server"
},
{
"command": "tsdetect.internal.workspace.deno.initializeWorkspace",
"command": "tsdetect.internal.deno.initializeWorkspace",
"title": "Initialize Deno Silently In Workspace Config",
"category": "Deno Language Server"
},
{
"command": "tsdetect.internal.workspace.node.initializeWorkspace",
"command": "tsdetect.internal.node.initializeWorkspace",
"title": "Initialize Node Silently In Workspace Config",
"category": "TypeScript Language Server"
}
Expand All @@ -86,82 +55,47 @@
"type": "string",
"default": "auto",
"markdownDescription": "How to setup `deno.enabled` and `tsserver.enabled`.\n\n- `\"auto\"` (default): Automatically switching `deno.enabled` and `tsserver.enabled` based on file environment. This is determined whether there is a node_modules directory in one of ancestor directories.\n- `\"auto_user_config\"`: TODO.\n- `\"auto_workspace_config\"`: TODO.\n- `\"manual\"`: coc-tsdetect won't do nothing.\n",
"enum": [
"auto",
"manual"
],
"examples": [
"auto",
"manual"
]
"enum": ["auto", "manual"],
"examples": ["auto", "manual"]
},
"tsdetect.configType": {
"type": "string",
"default": "ephemeral",
"markdownDescription": "Determine how configuration changed automatically.\n\n",
"enum": [
"ephemeral",
"workspace",
"user"
],
"examples": [
"ephemeral",
"workspace",
"user"
]
"tsdetect.doNotCreateOnNode": {
"type": "boolean",
"default": true,
"examples": [false, true]
},
"tsdetect.controlTrimSameExts": {
"tsdetect.doNothingIfConfigExists": {
"type": "boolean",
"default": true,
"markdownDescription": "Whether to control coc.source.file.trimSameExts.",
"examples": [
true,
false
]
"examples": [false, true]
},
"tsdetect.controlTrimSameExtsBase": {
"type": "array",
"items": {
"type": "string"
"tsdetect.nodeOverride": {
"type": "object",
"default": {
"deno.lint": false,
"coc.source.file.trimSameExts": [".js", ".ts"],
"prettier.disableLanguage": []
},
"default": [],
"markdownDescription": "When controlling coc.source.file.trimSameExts, these values are used as base.",
"examples": [
[]
{}
]
},
"tsdetect.controlTrimSameExtsNode": {
"type": "array",
"items": {
"type": "string"
},
"default": [
".ts",
".js"
],
"markdownDescription": "Extensions added when using Node environment to `tsdetect.controlTrimSameExtsBase`.",
"examples": [
[],
[
".ts",
".js"
],
[
".tsx",
".ts",
".js"
"tsdetect.denoOverride": {
"type": "object",
"default": {
"deno.lint": true,
"coc.source.file.trimSameExts": [],
"prettier.disableLanguage": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
]
]
},
"tsdetect.controlTrimSameExtsDeno": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"markdownDescription": "Extensions added when using Deno environment to `tsdetect.controlTrimSameExtsBase`. Maybe you would love to keep it default.",
"examples": [
[]
{},
{
"deno.unstable": true
}
]
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/coc_internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum ConfigurationTarget {
Global,
User,
Workspace,
}
87 changes: 47 additions & 40 deletions src/commands.ts
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`,
);
};
Loading

0 comments on commit 6393324

Please sign in to comment.