Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit d52fee6

Browse files
author
Rusty Key
committed
use temp files to better handle large files
1 parent a4c1821 commit d52fee6

File tree

4 files changed

+120
-76
lines changed

4 files changed

+120
-76
lines changed

package.json

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,24 @@
1616
"engines": {
1717
"vscode": "^1.21.0"
1818
},
19-
"categories": ["Formatters", "Programming Languages", "Linters", "Snippets"],
20-
"keywords": ["ocaml", "reason", "bucklescript", "reasonml", "merlin"],
19+
"categories": [
20+
"Formatters",
21+
"Programming Languages",
22+
"Linters",
23+
"Snippets"
24+
],
25+
"keywords": [
26+
"ocaml",
27+
"reason",
28+
"bucklescript",
29+
"reasonml",
30+
"merlin"
31+
],
2132
"icon": "assets/logo.png",
22-
"activationEvents": ["onLanguage:ocaml", "onLanguage:reason"],
33+
"activationEvents": [
34+
"onLanguage:ocaml",
35+
"onLanguage:reason"
36+
],
2337
"main": "./out/src/extension",
2438
"contributes": {
2539
"configuration": {
@@ -42,26 +56,34 @@
4256
"type": "integer"
4357
},
4458
{
45-
"enum": ["Infinity"]
59+
"enum": [
60+
"Infinity"
61+
]
4662
}
4763
],
4864
"default": 500,
49-
"description":
50-
"How long to idle (in milliseconds) after keypresses before refreshing linter diagnostics. Smaller values refresh diagnostics more quickly."
65+
"description": "How long to idle (in milliseconds) after keypresses before refreshing linter diagnostics. Smaller values refresh diagnostics more quickly."
5166
},
5267
"reason.diagnostics.tools": {
5368
"type": "array",
5469
"items": {
55-
"enum": ["merlin", "bsb"]
70+
"enum": [
71+
"merlin",
72+
"bsb"
73+
]
5674
},
57-
"default": ["merlin"],
75+
"default": [
76+
"merlin"
77+
],
5878
"maxItems": 2,
5979
"uniqueItems": true,
60-
"description":
61-
"Specifies which tool or tools will be used to get diagnostics. If you choose both \"merlin\" and \"bsb\", merlin will be used while editing and bsb when saving."
80+
"description": "Specifies which tool or tools will be used to get diagnostics. If you choose both \"merlin\" and \"bsb\", merlin will be used while editing and bsb when saving."
6281
},
6382
"reason.format.width": {
64-
"type": ["number", null],
83+
"type": [
84+
"number",
85+
null
86+
],
6587
"default": null,
6688
"description": "Set the width of lines when formatting code with refmt"
6789
},
@@ -83,8 +105,7 @@
83105
"reason.path.env": {
84106
"type": "string",
85107
"default": "env",
86-
"description":
87-
"The path to the `env` command which prints the language server environment for debugging editor issues."
108+
"description": "The path to the `env` command which prints the language server environment for debugging editor issues."
88109
},
89110
"reason.path.ocamlformat": {
90111
"type": "string",
@@ -129,9 +150,15 @@
129150
"reason.server.languages": {
130151
"type": "array",
131152
"items": {
132-
"enum": ["ocaml", "reason"]
153+
"enum": [
154+
"ocaml",
155+
"reason"
156+
]
133157
},
134-
"default": ["ocaml", "reason"],
158+
"default": [
159+
"ocaml",
160+
"reason"
161+
],
135162
"maxItems": 2,
136163
"uniqueItems": true,
137164
"description": "The list of languages enable support for in the language server."
@@ -192,15 +219,19 @@
192219
{
193220
"scopeName": "markdown.reason.codeblock",
194221
"path": "./syntaxes/reason-markdown-codeblock.json",
195-
"injectTo": ["text.html.markdown"],
222+
"injectTo": [
223+
"text.html.markdown"
224+
],
196225
"embeddedLanguages": {
197226
"meta.embedded.block.reason": "reason"
198227
}
199228
},
200229
{
201230
"scopeName": "markdown.ocaml.codeblock",
202231
"path": "./syntaxes/ocaml-markdown-codeblock.json",
203-
"injectTo": ["text.html.markdown"],
232+
"injectTo": [
233+
"text.html.markdown"
234+
],
204235
"embeddedLanguages": {
205236
"meta.embedded.block.ocaml": "ocaml"
206237
}
@@ -209,8 +240,13 @@
209240
"languages": [
210241
{
211242
"id": "ocaml",
212-
"aliases": ["OCaml"],
213-
"extensions": [".ml", ".mli"],
243+
"aliases": [
244+
"OCaml"
245+
],
246+
"extensions": [
247+
".ml",
248+
".mli"
249+
],
214250
"configuration": "./ocaml.configuration.json"
215251
},
216252
{
@@ -221,23 +257,40 @@
221257
},
222258
{
223259
"id": "ocaml.merlin",
224-
"aliases": ["Merlin"],
225-
"extensions": ["merlin"]
260+
"aliases": [
261+
"Merlin"
262+
],
263+
"extensions": [
264+
"merlin"
265+
]
226266
},
227267
{
228268
"id": "ocaml.ocamlbuild",
229-
"aliases": ["OCamlbuild"],
230-
"extensions": ["_tags"]
269+
"aliases": [
270+
"OCamlbuild"
271+
],
272+
"extensions": [
273+
"_tags"
274+
]
231275
},
232276
{
233277
"id": "ocaml.opam",
234-
"aliases": ["OPAM"],
235-
"extensions": ["opam"]
278+
"aliases": [
279+
"OPAM"
280+
],
281+
"extensions": [
282+
"opam"
283+
]
236284
},
237285
{
238286
"id": "reason",
239-
"aliases": ["Reason"],
240-
"extensions": [".re", ".rei"],
287+
"aliases": [
288+
"Reason"
289+
],
290+
"extensions": [
291+
".re",
292+
".rei"
293+
],
241294
"configuration": "./reason.configuration.json"
242295
},
243296
{
@@ -259,20 +312,20 @@
259312
"problemMatchers": [
260313
{
261314
"name": "ocamlc",
262-
263-
"fileLocation": ["relative", "${workspaceFolder}"],
315+
"fileLocation": [
316+
"relative",
317+
"${workspaceFolder}"
318+
],
264319
"pattern": [
265320
{
266-
"regexp":
267-
"^\\s*\\bFile\\b\\s*\"(.*)\",\\s*\\bline\\b\\s*(\\d+),\\s*\\bcharacters\\b\\s*(\\d+)-(\\d+)\\s*:\\s*$",
321+
"regexp": "^\\s*\\bFile\\b\\s*\"(.*)\",\\s*\\bline\\b\\s*(\\d+),\\s*\\bcharacters\\b\\s*(\\d+)-(\\d+)\\s*:\\s*$",
268322
"file": 1,
269323
"line": 2,
270324
"column": 3,
271325
"endColumn": 4
272326
},
273327
{
274-
"regexp":
275-
"^(?:\\s*\\bParse\\b\\s*)?\\s*\\b([Ee]rror|Warning)\\b\\s*(?:\\(\\s*\\bwarning\\b\\s*(\\d+)\\))?\\s*:\\s*(.*)$",
328+
"regexp": "^(?:\\s*\\bParse\\b\\s*)?\\s*\\b([Ee]rror|Warning)\\b\\s*(?:\\(\\s*\\bwarning\\b\\s*(\\d+)\\))?\\s*:\\s*(.*)$",
276329
"severity": 1,
277330
"code": 2,
278331
"message": 3
@@ -293,6 +346,7 @@
293346
"@types/lodash.flatmap": "^4.5.3",
294347
"@types/node": "9.6.2",
295348
"@types/pegjs": "0.10.0",
349+
"@types/uuid": "^3.4.4",
296350
"prettier": "1.11.1",
297351
"tslint": "5.9.1",
298352
"typescript": "2.8.1",
@@ -301,6 +355,7 @@
301355
"dependencies": {
302356
"lodash.flatmap": "^4.5.0",
303357
"pegjs": "0.10.0",
358+
"uuid": "^3.3.2",
304359
"vscode-jsonrpc": "3.6.0",
305360
"vscode-languageclient": "4.0.1",
306361
"vscode-languageserver": "4.0.0",

src/formatters/ocaml.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import { execSync } from "child_process";
2+
import * as fs from "fs";
23
import * as path from "path";
4+
import { v4 as uuidv4 } from "uuid";
35
import * as vscode from "vscode";
46
import { getFullTextRange } from "./utils";
57

68
export function register() {
79
const configuration = vscode.workspace.getConfiguration("ocaml-reason-format");
810
const rootPath = vscode.workspace.rootPath || "";
911

10-
vscode.languages.registerDocumentFormattingEditProvider("ocaml", {
12+
vscode.languages.registerDocumentFormattingEditProvider("ocaml", {
1113
provideDocumentFormattingEdits(_document: vscode.TextDocument): vscode.TextEdit[] {
1214
const formatterPath = configuration.get<string | undefined>("ocamlformat");
1315
const formatter = formatterPath ? path.resolve(rootPath, formatterPath) : "ocamlformat";
1416
const textEditor = vscode.window.activeTextEditor;
1517

1618
if (textEditor) {
17-
const text = textEditor.document.getText();
19+
const tempFileName = `/tmp/vscode-reasonml-${uuidv4()}`;
20+
fs.writeFileSync(tempFileName, textEditor.document.getText(), "utf8");
1821
const filePath = textEditor.document.fileName;
19-
const formattedText = execSync(`cd ${rootPath} && ${formatter} --name=${filePath} /dev/stdin <<<'${text}'`, {
20-
cwd: rootPath,
21-
}).toString();
22-
const textRange = getFullTextRange(textEditor);
22+
const formattedText = execSync(`cd ${rootPath} && ${formatter} --name=${filePath} ${tempFileName}`).toString();
23+
fs.unlinkSync(tempFileName);
2324

25+
const textRange = getFullTextRange(textEditor);
2426
return [vscode.TextEdit.replace(textRange, formattedText)];
2527
} else {
2628
return [];

src/formatters/reason.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { execSync } from "child_process";
2+
import * as fs from "fs";
23
import * as path from "path";
4+
import { v4 as uuidv4 } from "uuid";
35
import * as vscode from "vscode";
46
import { getFullTextRange } from "./utils";
57

@@ -14,9 +16,11 @@ export function register() {
1416
const textEditor = vscode.window.activeTextEditor;
1517

1618
if (textEditor) {
17-
const text = textEditor.document.getText();
18-
const formattedText = execSync(`${formatter} <<<'${text}'`).toString();
19+
const tempFileName = `/tmp/vscode-refmt-${uuidv4()}`;
20+
fs.writeFileSync(tempFileName, textEditor.document.getText(), "utf8");
21+
const formattedText = execSync(`${formatter} ${tempFileName}`).toString();
1922
const textRange = getFullTextRange(textEditor);
23+
fs.unlinkSync(tempFileName);
2024

2125
return [vscode.TextEdit.replace(textRange, formattedText)];
2226
} else {

yarn.lock

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
version "4.14.109"
1313
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.109.tgz#b1c4442239730bf35cabaf493c772b18c045886d"
1414

15+
"@types/node@*":
16+
version "11.9.6"
17+
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.9.6.tgz#c632bbcc780a1349673a6e2e9b9dfa8c369d3c74"
18+
integrity sha512-4hS2K4gwo9/aXIcoYxCtHpdgd8XUeDmo1siRCAH3RziXB65JlPqUFMtfy9VPj+og7dp3w1TFjGwYga4e0m9GwA==
19+
1520
1621
version "9.6.2"
1722
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.2.tgz#e49ac1adb458835e95ca6487bc20f916b37aff23"
@@ -20,6 +25,13 @@
2025
version "0.10.0"
2126
resolved "https://registry.yarnpkg.com/@types/pegjs/-/pegjs-0.10.0.tgz#a14736222e6208e1c68c0be231ab1c53ddc55392"
2227

28+
"@types/uuid@^3.4.4":
29+
version "3.4.4"
30+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.4.tgz#7af69360fa65ef0decb41fd150bf4ca5c0cefdf5"
31+
integrity sha512-tPIgT0GUmdJQNSHxp0X2jnpQfBSTfGxUMc/2CXBU2mnyTFVYVa2ojpoQ74w0U2yn2vw3jnC640+77lkFFpdVDw==
32+
dependencies:
33+
"@types/node" "*"
34+
2335
ajv@^5.1.0:
2436
version "5.5.2"
2537
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965"
@@ -130,12 +142,6 @@ assert-plus@^0.2.0:
130142
version "0.2.0"
131143
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-0.2.0.tgz#d74e1b87e7affc0db8aadb7021f3fe48101ab234"
132144

133-
134-
version "2.6.0"
135-
resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4"
136-
dependencies:
137-
lodash "^4.14.0"
138-
139145
asynckit@^0.4.0:
140146
version "0.4.0"
141147
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@@ -361,10 +367,6 @@ deep-assign@^1.0.0:
361367
dependencies:
362368
is-obj "^1.0.0"
363369

364-
365-
version "2.1.0"
366-
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-2.1.0.tgz#511a54fff405fc346f0240bb270a3e9533a31102"
367-
368370
delayed-stream@~1.0.0:
369371
version "1.0.0"
370372
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
@@ -1126,14 +1128,6 @@ lodash.templatesettings@^3.0.0:
11261128
lodash._reinterpolate "^3.0.0"
11271129
lodash.escape "^3.0.0"
11281130

1129-
[email protected], lodash@^4.14.0:
1130-
version "4.17.5"
1131-
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
1132-
1133-
1134-
version "1.5.3"
1135-
resolved "https://registry.yarnpkg.com/lokijs/-/lokijs-1.5.3.tgz#6952722ffa3049a55a5e1c10ee4a0947a3e5e19b"
1136-
11371131
map-stream@~0.1.0:
11381132
version "0.1.0"
11391133
resolved "https://registry.yarnpkg.com/map-stream/-/map-stream-0.1.0.tgz#e56aa94c4c8055a16404a0674b78f215f7c8e194"
@@ -1257,22 +1251,6 @@ object.omit@^2.0.0:
12571251
for-own "^0.1.4"
12581252
is-extendable "^0.1.1"
12591253

1260-
1261-
version "1.0.35"
1262-
resolved "https://registry.yarnpkg.com/ocaml-language-server/-/ocaml-language-server-1.0.35.tgz#e80dab2d40a73b0eb629efc724a7b98581f801d1"
1263-
dependencies:
1264-
async "2.6.0"
1265-
deepmerge "2.1.0"
1266-
glob "7.1.2"
1267-
lodash "4.17.5"
1268-
lokijs "1.5.3"
1269-
pegjs "0.10.0"
1270-
vscode-jsonrpc "3.6.0"
1271-
vscode-languageclient "4.0.1"
1272-
vscode-languageserver "4.0.0"
1273-
vscode-languageserver-protocol "3.6.0"
1274-
vscode-uri "1.0.3"
1275-
12761254
once@^1.3.0, once@^1.4.0:
12771255
version "1.4.0"
12781256
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
@@ -1763,6 +1741,11 @@ uuid@^3.0.0, uuid@^3.1.0:
17631741
version "3.2.1"
17641742
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"
17651743

1744+
uuid@^3.3.2:
1745+
version "3.3.2"
1746+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"
1747+
integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==
1748+
17661749
vali-date@^1.0.0:
17671750
version "1.0.0"
17681751
resolved "https://registry.yarnpkg.com/vali-date/-/vali-date-1.0.0.tgz#1b904a59609fb328ef078138420934f6b86709a6"
@@ -1878,7 +1861,7 @@ [email protected]:
18781861
vscode-languageserver-protocol "^3.6.0"
18791862
vscode-uri "^1.0.1"
18801863

1881-
vscode-uri@1.0.3, vscode-uri@^1.0.1:
1864+
vscode-uri@^1.0.1:
18821865
version "1.0.3"
18831866
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-1.0.3.tgz#631bdbf716dccab0e65291a8dc25c23232085a52"
18841867

0 commit comments

Comments
 (0)