@statiolake/coc-general-config 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 statiolake
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # coc-general-config
2
+
3
+ Use coc configuration framework more generally
4
+
5
+ ## Install
6
+
7
+ `:CocInstall coc-general-config`
8
+
9
+ ## Keymaps
10
+
11
+ `nmap <silent> <C-l> <Plug>(coc-coc-general-config-keymap)`
12
+
13
+ ## Lists
14
+
15
+ `:CocList demo_list`
16
+
17
+ ## License
18
+
19
+ MIT
20
+
21
+ ---
22
+
23
+ > This extension is built with [create-coc-extension](https://github.com/fannheyward/create-coc-extension)
package/lib/index.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ activate: () => activate
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_fs = require("fs");
37
+ var import_path = __toESM(require("path"));
38
+ var import_coc = require("coc.nvim");
39
+ var machineConfigFilename = "coc-machine-settings.json";
40
+ var channel = import_coc.window.createOutputChannel("coc-general-config");
41
+ var appliedKeys = /* @__PURE__ */ new Set();
42
+ async function machineConfigPath() {
43
+ const dataHome = process.env.COC_DATA_HOME ?? await import_coc.workspace.nvim.call("coc#util#get_data_home");
44
+ return import_path.default.join(dataHome, machineConfigFilename);
45
+ }
46
+ async function readMachineConfig() {
47
+ const filepath = await machineConfigPath();
48
+ try {
49
+ const parsed = JSON.parse(await import_fs.promises.readFile(filepath, "utf8"));
50
+ if (parsed == null || Array.isArray(parsed) || typeof parsed !== "object") {
51
+ throw new Error("top-level value must be an object");
52
+ }
53
+ return parsed;
54
+ } catch (error) {
55
+ if (error.code === "ENOENT")
56
+ return {};
57
+ throw new Error(`failed to read ${filepath}: ${String(error)}`);
58
+ }
59
+ }
60
+ async function applyMachineConfig() {
61
+ const config = await readMachineConfig();
62
+ const update = {};
63
+ for (const key of appliedKeys)
64
+ update[key] = void 0;
65
+ Object.assign(update, config);
66
+ import_coc.workspace.configurations.updateMemoryConfig(update);
67
+ appliedKeys = new Set(Object.keys(config));
68
+ channel.appendLine(`loaded ${appliedKeys.size} machine-local settings`);
69
+ }
70
+ async function openMachineConfig() {
71
+ const filepath = await machineConfigPath();
72
+ await import_fs.promises.mkdir(import_path.default.dirname(filepath), { recursive: true });
73
+ try {
74
+ await import_fs.promises.access(filepath);
75
+ } catch {
76
+ await import_fs.promises.writeFile(filepath, "{}\n");
77
+ }
78
+ const escaped = await import_coc.workspace.nvim.call("fnameescape", [filepath]);
79
+ await import_coc.workspace.nvim.command(`edit ${escaped}`);
80
+ }
81
+ async function activate(context) {
82
+ const reload = async () => {
83
+ try {
84
+ await applyMachineConfig();
85
+ } catch (error) {
86
+ channel.appendLine(String(error));
87
+ void import_coc.window.showErrorMessage(String(error));
88
+ }
89
+ };
90
+ context.subscriptions.push(
91
+ import_coc.commands.registerCommand("coc-general-config.openMachineConfig", openMachineConfig),
92
+ import_coc.commands.registerCommand("coc-general-config.reloadMachineConfig", reload),
93
+ import_coc.workspace.registerAutocmd({
94
+ event: "BufWritePost",
95
+ pattern: machineConfigFilename,
96
+ callback: reload
97
+ })
98
+ );
99
+ await import_coc.workspace.nvim.command(
100
+ "command! CocMachineConfig CocCommand coc-general-config.openMachineConfig"
101
+ );
102
+ await reload();
103
+ }
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ activate
107
+ });
@@ -0,0 +1,152 @@
1
+ local M = {}
2
+
3
+ local machine_config_filename = 'coc-machine-settings.json'
4
+
5
+ local function read_json_file(filepath)
6
+ local file = io.open(filepath, 'r')
7
+ if not file then
8
+ return nil
9
+ end
10
+ local content = file:read '*a'
11
+ file:close()
12
+ local ok, decoded = pcall(vim.fn.json_decode, content)
13
+ return ok and decoded or nil
14
+ end
15
+
16
+ local function unflatten_keys(tbl)
17
+ -- テーブルでない場合は変換不要
18
+ if type(tbl) ~= 'table' then
19
+ return tbl
20
+ end
21
+
22
+ local result = {}
23
+ for k, v in pairs(tbl) do
24
+ local parts = {}
25
+ -- キーをドットで分割
26
+ for part in k:gmatch '[^%.]+' do
27
+ table.insert(parts, part)
28
+ end
29
+
30
+ -- 入れ子構造を構築
31
+ local current = result
32
+ for i = 1, #parts - 1 do
33
+ local part = parts[i]
34
+ current[part] = current[part] or {}
35
+ current = current[part]
36
+ end
37
+ current[parts[#parts]] = v
38
+ end
39
+
40
+ return result
41
+ end
42
+
43
+ local function data_home()
44
+ if vim.g.coc_data_home then
45
+ return vim.fn.expand(vim.g.coc_data_home)
46
+ end
47
+ return vim.fn.stdpath 'data' .. '/coc'
48
+ end
49
+
50
+ function M.machine_config_path()
51
+ return data_home() .. '/' .. machine_config_filename
52
+ end
53
+
54
+ local function read_flat_config(path)
55
+ local config = read_json_file(path)
56
+ return type(config) == 'table' and config or {}
57
+ end
58
+
59
+ function M.apply_machine_config()
60
+ local machine_config = read_flat_config(M.machine_config_path())
61
+ vim.g.coc_user_config = vim.tbl_extend(
62
+ 'force',
63
+ vim.g.coc_user_config or {},
64
+ machine_config
65
+ )
66
+ end
67
+
68
+ function M.get(path)
69
+ -- Check if coc.nvim is initialized
70
+ if vim.g.coc_service_initialized == 1 then
71
+ return vim.fn['coc#util#get_config'](path)
72
+ end
73
+
74
+ -- Parse configurations manually
75
+ local user_config = unflatten_keys(vim.g.coc_user_config or {})
76
+
77
+ local global_config_path = vim.g.coc_config_home
78
+ and (vim.g.coc_config_home .. '/coc-settings.json')
79
+ or (vim.fn.stdpath 'config' .. '/coc-settings.json')
80
+ local global_config = read_flat_config(global_config_path)
81
+
82
+ local workspace_config_path = '.vim/coc-settings.json'
83
+ local workspace_config = read_flat_config(workspace_config_path)
84
+ local user_config = vim.g.coc_user_config or {}
85
+ local machine_config = read_flat_config(M.machine_config_path())
86
+
87
+ -- Merge configurations: machine > user > workspace > global.
88
+ local merged_config = vim.tbl_extend(
89
+ 'force',
90
+ global_config,
91
+ workspace_config,
92
+ user_config,
93
+ machine_config
94
+ )
95
+ merged_config = unflatten_keys(merged_config)
96
+
97
+ -- Remove filetype-specific settings
98
+ for key, _ in pairs(merged_config) do
99
+ if key:match '^%[.*%]$' then
100
+ merged_config[key] = nil
101
+ end
102
+ end
103
+
104
+ -- Extract settings based on the path
105
+ if not path then
106
+ return merged_config
107
+ end
108
+
109
+ -- Split the path and traverse the configuration tree
110
+ local parts = {}
111
+ for part in path:gmatch '[^%.]+' do
112
+ table.insert(parts, part)
113
+ end
114
+
115
+ local current = merged_config
116
+ for _, part in ipairs(parts) do
117
+ if type(current) ~= 'table' then
118
+ return nil
119
+ end
120
+ current = current[part]
121
+ end
122
+
123
+ if type(current) ~= 'table' then
124
+ -- NOTE: Not ideal but matches coc#util#get_config() behavior
125
+ return nil
126
+ end
127
+ return current
128
+ end
129
+
130
+ function M.set(path, value)
131
+ if vim.g.coc_service_initialized == 1 then
132
+ -- Use coc#config() if available
133
+ return vim.fn['coc#config'](path, value)
134
+ end
135
+
136
+ local user_config = vim.g.coc_user_config or {}
137
+ user_config[path] = value
138
+ vim.g.coc_user_config = user_config
139
+ return true
140
+ end
141
+
142
+ function M.access(path, value)
143
+ if value == nil then
144
+ return M.get(path)
145
+ else
146
+ return M.set(path, value)
147
+ end
148
+ end
149
+
150
+ M.apply_machine_config()
151
+
152
+ return M
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@statiolake/coc-general-config",
3
+ "version": "0.1.0",
4
+ "description": "Use coc configuration framework more generally",
5
+ "author": "statiolake <20490597+statiolake@users.noreply.github.com>",
6
+ "license": "MIT",
7
+ "main": "lib/index.js",
8
+ "keywords": [
9
+ "coc.nvim"
10
+ ],
11
+ "engines": {
12
+ "coc": "^0.0.82"
13
+ },
14
+ "scripts": {
15
+ "watch": "node esbuild.mjs --watch",
16
+ "build": "node esbuild.mjs",
17
+ "prepare": "node esbuild.mjs"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^16.18.0",
21
+ "coc.nvim": "^0.0.83-next.24",
22
+ "esbuild": "^0.19.8",
23
+ "typescript": "^5.3.3"
24
+ },
25
+ "activationEvents": [
26
+ "*"
27
+ ],
28
+ "contributes": {
29
+ "configuration": {
30
+ "type": "object",
31
+ "title": "coc-general-config configuration",
32
+ "properties": {
33
+ "coc-general-config.enabled": {
34
+ "type": "boolean",
35
+ "default": true,
36
+ "description": "Enable coc-general-config extension"
37
+ }
38
+ }
39
+ },
40
+ "commands": [
41
+ {
42
+ "command": "coc-general-config.openMachineConfig",
43
+ "title": "Open machine-local Coc settings"
44
+ },
45
+ {
46
+ "command": "coc-general-config.reloadMachineConfig",
47
+ "title": "Reload machine-local Coc settings"
48
+ }
49
+ ]
50
+ },
51
+ "files": [
52
+ "lib",
53
+ "lua"
54
+ ],
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "git+https://github.com/statiolake/coc-general-config.git"
61
+ },
62
+ "bugs": {
63
+ "url": "https://github.com/statiolake/coc-general-config/issues"
64
+ },
65
+ "homepage": "https://github.com/statiolake/coc-general-config#readme"
66
+ }