-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (122 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* --------------------------------------------------------
* https://github.com/neovim/node-client
* :UpdateRemotePlugins
* !AND RESTART!
* -- DEBUGGING --
* export NVIM_NODE_LOG_LEVEL=debug
* export NVIM_NODE_LOG_FILE=/home/jd/neo_dev_log/nvim.log
* --------------------------------------------------------
*/
const schema = require('./lib/schema.js');
const LINE_SEP = '\n';
const linesToString = (lines) => {
return lines.join(LINE_SEP);
}
const stringToLines = (str) => {
return str.split(LINE_SEP);
}
const jsonToJs = (str) => {
return new Promise(resolve => {
resolve(JSON.parse(str));
})
};
const jsToJson = (str) => {
return new Promise(resolve => {
resolve(JSON.stringify(str, null, ' '));
})
};
const writeOut = async (plugin, str) => {
try {
await plugin.nvim.outWrite(str + '\n');
} catch (err) {
console.error(err);
}
};
module.exports = (plugin) => {
// plugin.setOptions({ dev: true, alwaysInit: true });
plugin.setOptions({ dev: false, alwaysInit: true });
// JsonFormat
plugin.registerCommand('JsonFormat', async () => {
const buffer = plugin.nvim.buffer;
buffer.lines.then((lines) => {
if (lines.length === 0) {
return;
}
let lines_length = lines.length;
let s = linesToString(lines);
let o = jsonToJs(s).then((o) => {
let s = JSON.stringify(o, null, ' ')
let lines = stringToLines(s);
if (lines.length > 0) {
buffer.remove(0, lines_length, true);
buffer.insert(lines, 0);
}
}).catch((err) => {
writeOut(plugin, 'invalid JSON');
});
});
}, { sync: false, nargs: '*', range: ''});
// json2Schema
plugin.registerCommand('Json2Schema', async () => {
let buffer = plugin.nvim.buffer;
buffer.lines.then((lines) => {
if (lines.length === 0) {
return;
}
let lines_length = lines.length;
let s = linesToString(lines);
let o = jsonToJs(s).then((o) => {
// Get an object name
plugin.nvim.call('input', 'object name? ').then((xx) => {
if (xx === '') {
xx = 'JsonSchema';
}
writeOut(plugin, '');
let a = schema.toSchema(xx ,o);
if (a) {
let lines = stringToLines(a);
if (lines.length > 0) {
buffer.remove(0, lines_length, true);
buffer.insert(lines, 0);
}
}
});
}).catch((err) => {
console.error(err);
writeOut(plugin, 'invalid JSON');
});
});
}, { sync: false });
// Json2JSDoc
plugin.registerCommand('Json2JSDoc', async () => {
const buffer = plugin.nvim.buffer;
buffer.lines.then((lines) => {
if (lines.length === 0) {
return;
}
const lines_length = lines.length;
const s = linesToString(lines);
jsonToJs(s).then((o) => {
plugin.nvim.call('input', 'object name? ').then((xx) => {
if (xx === '') {
xx = 'JsonSchema';
}
writeOut(plugin, '');
const a = schema.toJsDoc(xx, o);
if (a) {
const lines = stringToLines(a);
if (lines.length > 0) {
buffer.remove(0, lines_length, true);
buffer.insert(lines, 0);
}
}
});
}).catch((err) => {
console.error(err);
writeOut(plugin, 'invalid JSON');
});
});
}, { sync: false });
};
// vim: set ts=2 sw=2 et :