Skip to content

Commit b29e99f

Browse files
committed
chore: ran prettier
1 parent e957b1a commit b29e99f

File tree

3 files changed

+95
-90
lines changed

3 files changed

+95
-90
lines changed

lib/adapters/apply-edit-adapter.ts

+36-30
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
CreateFile,
1010
RenameFile,
1111
DeleteFile,
12-
DocumentUri
12+
DocumentUri,
1313
} from "../languageclient"
1414
import { TextBuffer, TextEditor } from "atom"
1515
import * as fs from "fs"
@@ -51,24 +51,26 @@ export default class ApplyEditAdapter {
5151
// Keep checkpoints from all successful buffer edits
5252
const checkpoints: Array<{ buffer: TextBuffer; checkpoint: number }> = []
5353

54-
const promises = (workspaceEdit.documentChanges || []).map(async (edit): Promise<void> => {
55-
if (!TextDocumentEdit.is(edit)) {
56-
return ApplyEditAdapter.handleResourceOperation(edit).catch((err) => {
57-
throw Error(`Error during ${edit.kind} resource operation: ${err.message}`)
58-
})
54+
const promises = (workspaceEdit.documentChanges || []).map(
55+
async (edit): Promise<void> => {
56+
if (!TextDocumentEdit.is(edit)) {
57+
return ApplyEditAdapter.handleResourceOperation(edit).catch((err) => {
58+
throw Error(`Error during ${edit.kind} resource operation: ${err.message}`)
59+
})
60+
}
61+
const path = Convert.uriToPath(edit.textDocument.uri)
62+
const editor = (await atom.workspace.open(path, {
63+
searchAllPanes: true,
64+
// Open new editors in the background.
65+
activatePane: false,
66+
activateItem: false,
67+
})) as TextEditor
68+
const buffer = editor.getBuffer()
69+
const edits = Convert.convertLsTextEdits(edit.edits)
70+
const checkpoint = ApplyEditAdapter.applyEdits(buffer, edits)
71+
checkpoints.push({ buffer, checkpoint })
5972
}
60-
const path = Convert.uriToPath(edit.textDocument.uri)
61-
const editor = (await atom.workspace.open(path, {
62-
searchAllPanes: true,
63-
// Open new editors in the background.
64-
activatePane: false,
65-
activateItem: false,
66-
})) as TextEditor
67-
const buffer = editor.getBuffer()
68-
const edits = Convert.convertLsTextEdits(edit.edits)
69-
const checkpoint = ApplyEditAdapter.applyEdits(buffer, edits)
70-
checkpoints.push({ buffer, checkpoint })
71-
})
73+
)
7274

7375
// Apply all edits or fail and revert everything
7476
const applied = await Promise.all(promises)
@@ -87,22 +89,20 @@ export default class ApplyEditAdapter {
8789
return { applied }
8890
}
8991

90-
private static async handleResourceOperation(edit: (CreateFile | RenameFile | DeleteFile)): Promise<void> {
92+
private static async handleResourceOperation(edit: CreateFile | RenameFile | DeleteFile): Promise<void> {
9193
if (DeleteFile.is(edit)) {
9294
const path = Convert.uriToPath(edit.uri)
93-
const exists = await fs.promises.stat(path).then(() => true).catch(() => false)
95+
const stats: boolean | fs.Stats = await fs.promises.lstat(path).catch(() => false)
9496
const ignoreIfNotExists = edit.options?.ignoreIfNotExists
9597

96-
if (!exists) {
98+
if (!stats) {
9799
if (ignoreIfNotExists) {
98100
return
99101
}
100102
throw Error(`Target doesn't exist.`)
101103
}
102104

103-
const isDirectory = fs.lstatSync(path).isDirectory()
104-
105-
if (isDirectory) {
105+
if (stats.isDirectory()) {
106106
if (edit.options?.recursive) {
107107
return new Promise((resolve, reject) => {
108108
rimraf(path, { glob: false }, (err) => {
@@ -121,7 +121,10 @@ export default class ApplyEditAdapter {
121121
if (RenameFile.is(edit)) {
122122
const oldPath = Convert.uriToPath(edit.oldUri)
123123
const newPath = Convert.uriToPath(edit.newUri)
124-
const exists = await fs.promises.stat(newPath).then(() => true).catch(() => false)
124+
const exists = await fs.promises
125+
.access(newPath)
126+
.then(() => true)
127+
.catch(() => false)
125128
const ignoreIfExists = edit.options?.ignoreIfExists
126129
const overwrite = edit.options?.overwrite
127130

@@ -137,29 +140,32 @@ export default class ApplyEditAdapter {
137140
}
138141
if (CreateFile.is(edit)) {
139142
const path = Convert.uriToPath(edit.uri)
140-
const exists = await fs.promises.stat(path).then(() => true).catch(() => false)
143+
const exists = await fs.promises
144+
.access(path)
145+
.then(() => true)
146+
.catch(() => false)
141147
const ignoreIfExists = edit.options?.ignoreIfExists
142148
const overwrite = edit.options?.overwrite
143149

144150
if (exists && ignoreIfExists && !overwrite) {
145151
return
146152
}
147153

148-
return fs.promises.writeFile(path, '')
154+
return fs.promises.writeFile(path, "")
149155
}
150156
}
151157

152158
private static normalize(workspaceEdit: WorkspaceEdit): void {
153159
const documentChanges = workspaceEdit.documentChanges || []
154160

155-
if (!('documentChanges' in workspaceEdit) && ('changes' in workspaceEdit)) {
161+
if (!("documentChanges" in workspaceEdit) && "changes" in workspaceEdit) {
156162
Object.keys(workspaceEdit.changes || []).forEach((uri: DocumentUri) => {
157163
documentChanges.push({
158164
textDocument: {
159165
version: null,
160-
uri: uri
166+
uri,
161167
},
162-
edits: workspaceEdit.changes![uri]
168+
edits: workspaceEdit.changes![uri],
163169
})
164170
})
165171
}

lib/auto-languageclient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export default class AutoLanguageClient {
122122
documentChanges: true,
123123
normalizesLineEndings: false,
124124
changeAnnotationSupport: undefined,
125-
resourceOperations: ["create", "rename", "delete"]
125+
resourceOperations: ["create", "rename", "delete"],
126126
},
127127
workspaceFolders: false,
128128
didChangeConfiguration: {

test/adapters/apply-edit-adapter.test.ts

+58-59
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const TEST_PATH1 = normalizeDriveLetterName(path.join(__dirname, "test.txt"))
1111
const TEST_PATH2 = normalizeDriveLetterName(path.join(__dirname, "test2.txt"))
1212
const TEST_PATH3 = normalizeDriveLetterName(path.join(__dirname, "test3.txt"))
1313
const TEST_PATH4 = normalizeDriveLetterName(path.join(__dirname, "test4.txt"))
14-
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atom-languageclient-tests'))
14+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "atom-languageclient-tests"))
1515

1616
function normalizeDriveLetterName(filePath: string): string {
1717
if (process.platform === "win32") {
@@ -194,105 +194,105 @@ describe("ApplyEditAdapter", () => {
194194
const directory = fs.mkdtempSync(tempDir)
195195
const oldUri = path.join(directory, "test.txt")
196196
const newUri = path.join(directory, "test-renamed.txt")
197-
fs.writeFileSync(oldUri, 'abcd')
197+
fs.writeFileSync(oldUri, "abcd")
198198

199199
const result = await ApplyEditAdapter.onApplyEdit({
200200
edit: {
201201
documentChanges: [
202202
{
203203
kind: "rename",
204-
oldUri: oldUri,
205-
newUri: newUri,
206-
}
207-
]
204+
oldUri,
205+
newUri,
206+
},
207+
],
208208
},
209209
})
210210

211211
expect(result.applied).to.equal(true)
212212
expect(fs.existsSync(newUri)).to.equal(true)
213-
expect(fs.readFileSync(newUri).toString()).to.equal('abcd')
213+
expect(fs.readFileSync(newUri).toString()).to.equal("abcd")
214214
expect(fs.existsSync(oldUri)).to.equal(false)
215215
})
216216

217217
it("handles rename operation with ignoreIfExists option", async () => {
218218
const directory = fs.mkdtempSync(tempDir)
219219
const oldUri = path.join(directory, "test.txt")
220220
const newUri = path.join(directory, "test-renamed.txt")
221-
fs.writeFileSync(oldUri, 'abcd')
222-
fs.writeFileSync(newUri, 'efgh')
221+
fs.writeFileSync(oldUri, "abcd")
222+
fs.writeFileSync(newUri, "efgh")
223223

224224
const result = await ApplyEditAdapter.onApplyEdit({
225225
edit: {
226226
documentChanges: [
227227
{
228228
kind: "rename",
229-
oldUri: oldUri,
230-
newUri: newUri,
229+
oldUri,
230+
newUri,
231231
options: {
232-
ignoreIfExists: true
233-
}
234-
}
235-
]
232+
ignoreIfExists: true,
233+
},
234+
},
235+
],
236236
},
237237
})
238238

239239
expect(result.applied).to.equal(true)
240240
expect(fs.existsSync(oldUri)).to.equal(true)
241-
expect(fs.readFileSync(newUri).toString()).to.equal('efgh')
241+
expect(fs.readFileSync(newUri).toString()).to.equal("efgh")
242242
})
243243

244244
it("handles rename operation with overwrite option", async () => {
245245
const directory = fs.mkdtempSync(tempDir)
246246
const oldUri = path.join(directory, "test.txt")
247247
const newUri = path.join(directory, "test-renamed.txt")
248-
fs.writeFileSync(oldUri, 'abcd')
249-
fs.writeFileSync(newUri, 'efgh')
248+
fs.writeFileSync(oldUri, "abcd")
249+
fs.writeFileSync(newUri, "efgh")
250250

251251
const result = await ApplyEditAdapter.onApplyEdit({
252252
edit: {
253253
documentChanges: [
254254
{
255255
kind: "rename",
256-
oldUri: oldUri,
257-
newUri: newUri,
256+
oldUri,
257+
newUri,
258258
options: {
259259
overwrite: true,
260-
ignoreIfExists: true // Overwrite wins over ignoreIfExists
261-
}
262-
}
263-
]
260+
ignoreIfExists: true, // Overwrite wins over ignoreIfExists
261+
},
262+
},
263+
],
264264
},
265265
})
266266

267267
expect(result.applied).to.equal(true)
268268
expect(fs.existsSync(oldUri)).to.equal(false)
269-
expect(fs.readFileSync(newUri).toString()).to.equal('abcd')
269+
expect(fs.readFileSync(newUri).toString()).to.equal("abcd")
270270
})
271271

272272
it("throws an error on rename operation if target exists", async () => {
273273
const directory = fs.mkdtempSync(tempDir)
274274
const oldUri = path.join(directory, "test.txt")
275275
const newUri = path.join(directory, "test-renamed.txt")
276-
fs.writeFileSync(oldUri, 'abcd')
277-
fs.writeFileSync(newUri, 'efgh')
276+
fs.writeFileSync(oldUri, "abcd")
277+
fs.writeFileSync(newUri, "efgh")
278278

279279
const result = await ApplyEditAdapter.onApplyEdit({
280280
edit: {
281281
documentChanges: [
282282
{
283283
kind: "rename",
284-
oldUri: oldUri,
285-
newUri: newUri,
286-
}
287-
]
284+
oldUri,
285+
newUri,
286+
},
287+
],
288288
},
289289
})
290290

291291
expect(result.applied).to.equal(false)
292292
expect(fs.existsSync(oldUri)).to.equal(true)
293-
expect(fs.readFileSync(oldUri).toString()).to.equal('abcd')
293+
expect(fs.readFileSync(oldUri).toString()).to.equal("abcd")
294294
expect(fs.existsSync(newUri)).to.equal(true)
295-
expect(fs.readFileSync(newUri).toString()).to.equal('efgh')
295+
expect(fs.readFileSync(newUri).toString()).to.equal("efgh")
296296

297297
expect(
298298
(atom as any).notifications.addError.calledWith("workspace/applyEdits failed", {
@@ -305,16 +305,16 @@ describe("ApplyEditAdapter", () => {
305305
it("handles delete resource operations on files", async () => {
306306
const directory = fs.mkdtempSync(tempDir)
307307
const uri = path.join(directory, "test.txt")
308-
fs.writeFileSync(uri, 'abcd')
308+
fs.writeFileSync(uri, "abcd")
309309

310310
const result = await ApplyEditAdapter.onApplyEdit({
311311
edit: {
312312
documentChanges: [
313313
{
314314
kind: "delete",
315-
uri: uri
316-
}
317-
]
315+
uri,
316+
},
317+
],
318318
},
319319
})
320320

@@ -324,10 +324,10 @@ describe("ApplyEditAdapter", () => {
324324

325325
it("handles delete resource operations on directories", async () => {
326326
const directory = fs.mkdtempSync(tempDir)
327-
const file1 = path.join(directory, '1.txt')
328-
const file2 = path.join(directory, '2.txt')
329-
fs.writeFileSync(file1, '1')
330-
fs.writeFileSync(file2, '2')
327+
const file1 = path.join(directory, "1.txt")
328+
const file2 = path.join(directory, "2.txt")
329+
fs.writeFileSync(file1, "1")
330+
fs.writeFileSync(file2, "2")
331331

332332
const result = await ApplyEditAdapter.onApplyEdit({
333333
edit: {
@@ -336,10 +336,10 @@ describe("ApplyEditAdapter", () => {
336336
kind: "delete",
337337
uri: directory,
338338
options: {
339-
recursive: true
340-
}
341-
}
342-
]
339+
recursive: true,
340+
},
341+
},
342+
],
343343
},
344344
})
345345

@@ -351,10 +351,10 @@ describe("ApplyEditAdapter", () => {
351351

352352
it("throws an error when deleting a non-empty directory without recursive option", async () => {
353353
const directory = fs.mkdtempSync(tempDir)
354-
const file1 = path.join(directory, '1.txt')
355-
const file2 = path.join(directory, '2.txt')
356-
fs.writeFileSync(file1, '1')
357-
fs.writeFileSync(file2, '2')
354+
const file1 = path.join(directory, "1.txt")
355+
const file2 = path.join(directory, "2.txt")
356+
fs.writeFileSync(file1, "1")
357+
fs.writeFileSync(file2, "2")
358358

359359
const result = await ApplyEditAdapter.onApplyEdit({
360360
edit: {
@@ -363,10 +363,10 @@ describe("ApplyEditAdapter", () => {
363363
kind: "delete",
364364
uri: directory,
365365
options: {
366-
recursive: false
367-
}
368-
}
369-
]
366+
recursive: false,
367+
},
368+
},
369+
],
370370
},
371371
})
372372

@@ -379,16 +379,15 @@ describe("ApplyEditAdapter", () => {
379379
expect(errorCalls[0].args[1].detail).to.match(/Error during delete resource operation: (.*)/)
380380
})
381381

382-
383382
it("throws an error on delete operation if target doesnt exist", async () => {
384383
const result = await ApplyEditAdapter.onApplyEdit({
385384
edit: {
386385
documentChanges: [
387386
{
388387
kind: "delete",
389388
uri: path.join(tempDir, "unexisting.txt"),
390-
}
391-
]
389+
},
390+
],
392391
},
393392
})
394393
//
@@ -410,9 +409,9 @@ describe("ApplyEditAdapter", () => {
410409
documentChanges: [
411410
{
412411
kind: "create",
413-
uri: uri
414-
}
415-
]
412+
uri,
413+
},
414+
],
416415
},
417416
})
418417

0 commit comments

Comments
 (0)