-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
packageManagerCommon.ts
88 lines (83 loc) · 3.32 KB
/
packageManagerCommon.ts
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
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { FormattingOptions, Segment } from '@sqs/jsonc-parser'
import { setProperty } from '@sqs/jsonc-parser/lib/edit'
import path from 'path'
import * as sourcegraph from 'sourcegraph'
import { parseRepoURI } from '../../../../../shared/src/util/url'
import { ExecServerClient } from '../execServer/client'
import { Observable, combineLatest, from, of } from 'rxjs'
import { switchMap, map } from 'rxjs/operators'
export const editForCommands2 = (
files: (URL | Pick<sourcegraph.TextDocument, 'uri' | 'text'>)[],
commands: string[][],
execServerClient: ExecServerClient
): Observable<sourcegraph.WorkspaceEdit> =>
combineLatest(
files.map(file =>
file instanceof URL
? from(sourcegraph.workspace.openTextDocument(file))
: of<Pick<sourcegraph.TextDocument, 'uri' | 'text'>>(file)
)
).pipe(
switchMap(files => {
const dir = path.dirname(parseRepoURI(files[0].uri).filePath!)
const filesToText: { [path: string]: string } = {}
for (const file of files) {
const name = path.basename(parseRepoURI(file.uri).filePath!)
filesToText[name] = file.text!
}
return from(
execServerClient({
commands,
dir,
files: filesToText,
label: `editForCommands(${JSON.stringify({ files: files.map(f => f.uri), commands })})`,
})
).pipe(
map(result => {
const edit = new sourcegraph.WorkspaceEdit()
for (const file of files) {
const name = path.basename(parseRepoURI(file.uri).filePath!)
edit.set(new URL(file.uri), [sourcegraph.TextEdit.patch(result.fileDiffs![name])])
}
return edit
})
)
})
)
// function computeDiffs(files: { old: sourcegraph.TextDocument; newText?: string }[]): sourcegraph.WorkspaceEdit {
// const edit = new sourcegraph.WorkspaceEdit()
// for (const { old, newText } of files) {
// // TODO!(sqs): handle creation/removal
// if (old.text !== undefined && newText !== undefined && old.text !== newText) {
// edit.replace(
// new URL(old.uri),
// new sourcegraph.Range(new sourcegraph.Position(0, 0), old.positionAt(old.text.length)),
// newText
// )
// }
// }
// return edit
// }
const PACKAGE_JSON_FORMATTING_OPTIONS: FormattingOptions = {
eol: '\n',
insertSpaces: true,
tabSize: 2,
}
export const editPackageJson = (
doc: sourcegraph.TextDocument,
operations: { path: Segment[]; value: any }[],
workspaceEdit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit => {
for (const op of operations) {
const edits = setProperty(doc.text!, op.path, op.value, PACKAGE_JSON_FORMATTING_OPTIONS)
for (const edit of edits) {
workspaceEdit.replace(
new URL(doc.uri),
new sourcegraph.Range(doc.positionAt(edit.offset), doc.positionAt(edit.offset + edit.length)),
edit.content
)
}
}
return workspaceEdit
}