|
1 | | -import { Injectable, Autowired } from '@ali/common-di' |
2 | | -import { |
3 | | - IEditorDocumentModelSaveResult, |
4 | | - URI, |
5 | | - IEditorDocumentChange, |
6 | | - BasicTextLines, |
7 | | - isEditChange, |
8 | | - Uri, |
9 | | -} from '@ali/ide-core-common' |
10 | | -import { IDiskFileProvider, FileAccess } from '@ali/ide-file-service' |
11 | | -import md5 from 'md5' |
12 | | -import { |
13 | | - IFileSchemeDocNodeService, |
14 | | - ISavingContent, |
15 | | - IContentChange, |
16 | | -} from '@ali/ide-file-scheme/lib/common' |
| 1 | +import { Injectable, Autowired } from '@ali/common-di'; |
| 2 | +import { IEditorDocumentModelSaveResult, URI, IEditorDocumentChange, BasicTextLines, isEditChange } from '@ali/ide-core-common'; |
| 3 | +import { IFileService } from '@ali/ide-file-service/lib/common'; |
| 4 | +import md5 from 'md5'; |
| 5 | +import { IFileSchemeDocNodeService, ISavingContent, IContentChange } from '@ali/ide-file-scheme/lib/common'; |
| 6 | +import { fse } from '../node'; |
| 7 | +import { encode, decode } from '../file-service/encoding' |
17 | 8 |
|
18 | | -import { fse, buffer } from '../node' |
19 | | - |
20 | | -const { existsSync, readFile, statSync, writeFile } = fse |
21 | | -const { Buffer } = buffer |
22 | | - |
23 | | -// TODO: |
24 | | -// 只处理 file 协议,其它协议暂不支持 |
25 | 9 | @Injectable() |
26 | 10 | export class FileSchemeDocNodeServiceImpl implements IFileSchemeDocNodeService { |
27 | | - @Autowired(IDiskFileProvider) |
28 | | - private fileService: IDiskFileProvider |
| 11 | + @Autowired(IFileService) |
| 12 | + private fileService: IFileService; |
| 13 | + |
| 14 | + // 由于此处只处理file协议,为了简洁,不再使用 fileService, |
29 | 15 |
|
30 | | - async $saveByChange( |
31 | | - uri: string, |
32 | | - change: IContentChange, |
33 | | - encoding?: BufferEncoding | undefined, |
34 | | - force: boolean = false |
35 | | - ): Promise<IEditorDocumentModelSaveResult> { |
| 16 | + async $saveByChange(uri: string, change: IContentChange, encoding?: string | undefined, force: boolean = false): Promise<IEditorDocumentModelSaveResult> { |
36 | 17 | try { |
37 | | - const fsPath = new URI(uri).codeUri.fsPath |
38 | | - if (existsSync(fsPath)) { |
39 | | - const mtime = statSync(fsPath).mtime.getTime() |
40 | | - const contentBuffer = await readFile(fsPath) |
41 | | - const content = contentBuffer.toString(encoding ? encoding : 'utf8') |
| 18 | + const fsPath = new URI(uri).codeUri.path; |
| 19 | + if (await fse.pathExists(fsPath)) { |
| 20 | + const mtime = (await fse.stat(fsPath)).mtime.getTime(); |
| 21 | + const contentBuffer = await fse.readFile(fsPath); |
| 22 | + const content = decode(contentBuffer, encoding ? encoding : 'utf8'); |
42 | 23 | if (!force) { |
43 | | - const currentMd5 = md5(content) |
| 24 | + const currentMd5 = md5(content); |
44 | 25 | if (change.baseMd5 !== currentMd5) { |
45 | 26 | return { |
46 | 27 | state: 'diff', |
47 | | - } |
| 28 | + }; |
48 | 29 | } |
49 | 30 | } |
50 | | - const contentRes = applyChanges(content, change.changes!, change.eol) |
51 | | - if (statSync(fsPath).mtime.getTime() !== mtime) { |
52 | | - throw new Error('File has been modified during saving, please retry') |
| 31 | + const contentRes = applyChanges(content, change.changes!, change.eol); |
| 32 | + if ((await fse.stat(fsPath)).mtime.getTime() !== mtime) { |
| 33 | + throw new Error('File has been modified during saving, please retry'); |
53 | 34 | } |
54 | | - await writeFile(fsPath, Buffer.from(contentRes, encoding ? encoding : 'utf8')) |
| 35 | + await fse.writeFile(fsPath, encode(contentRes, encoding ? encoding : 'utf8')); |
55 | 36 | return { |
56 | 37 | state: 'success', |
57 | | - } |
58 | | - } else { |
59 | | - return { |
60 | | - state: 'error', |
61 | | - errorMessage: 'useByContent', |
62 | | - } |
| 38 | + }; |
| 39 | + } |
| 40 | + return { |
| 41 | + state: 'error', |
| 42 | + errorMessage: 'useByContent', |
63 | 43 | } |
64 | 44 | } catch (e) { |
65 | 45 | return { |
66 | 46 | state: 'error', |
67 | 47 | errorMessage: e.toString(), |
68 | | - } |
| 48 | + }; |
69 | 49 | } |
70 | 50 | } |
71 | 51 |
|
72 | | - async $saveByContent( |
73 | | - uri: string, |
74 | | - content: ISavingContent, |
75 | | - encoding?: string | undefined, |
76 | | - force: boolean = false |
77 | | - ): Promise<IEditorDocumentModelSaveResult> { |
| 52 | + async $saveByContent(uri: string, content: ISavingContent, encoding?: string | undefined, force: boolean = false): Promise<IEditorDocumentModelSaveResult> { |
78 | 53 | try { |
79 | | - const _uri = Uri.parse(uri) |
80 | | - const stat = await this.fileService.stat(_uri) |
| 54 | + const stat = await this.fileService.getFileStat(uri); |
81 | 55 | if (stat) { |
82 | 56 | if (!force) { |
83 | | - const res = await this.fileService.readFile(_uri, encoding) |
84 | | - if (content.baseMd5 !== md5(res)) { |
| 57 | + const res = await this.fileService.resolveContent(uri, {encoding}); |
| 58 | + if (content.baseMd5 !== md5(res.content)) { |
85 | 59 | return { |
86 | 60 | state: 'diff', |
87 | | - } |
| 61 | + }; |
88 | 62 | } |
89 | 63 | } |
90 | | - await this.fileService.writeFile(_uri, content.content, { |
91 | | - create: true, |
92 | | - overwrite: false, |
93 | | - encoding, |
94 | | - }) |
95 | | - return { |
96 | | - state: 'success', |
97 | | - } |
98 | | - } else { |
99 | | - await this.fileService.writeFile(_uri, content.content, { |
100 | | - create: true, |
101 | | - overwrite: false, |
102 | | - encoding, |
103 | | - }) |
| 64 | + await this.fileService.setContent(stat, content.content, {encoding}); |
104 | 65 | return { |
105 | 66 | state: 'success', |
106 | | - } |
| 67 | + }; |
107 | 68 | } |
| 69 | + await this.fileService.createFile(uri, {content: content.content, encoding}); |
| 70 | + return { |
| 71 | + state: 'success', |
| 72 | + }; |
108 | 73 | } catch (e) { |
109 | 74 | return { |
110 | 75 | state: 'error', |
111 | 76 | errorMessage: e.toString(), |
112 | | - } |
| 77 | + }; |
113 | 78 | } |
114 | 79 | } |
115 | 80 |
|
116 | 81 | async $getMd5(uri: string, encoding?: string | undefined): Promise<string | undefined> { |
117 | 82 | try { |
118 | | - const _uri = Uri.parse(uri) |
119 | | - if (await this.fileService.access(_uri, FileAccess.Constants.F_OK)) { |
120 | | - const res = await this.fileService.readFile(_uri, encoding) |
121 | | - return md5(res) |
122 | | - } else { |
123 | | - return undefined |
| 83 | + if (await this.fileService.access(uri)) { |
| 84 | + const res = await this.fileService.resolveContent(uri, {encoding}); |
| 85 | + return md5(res.content); |
124 | 86 | } |
125 | 87 | } catch (e) { |
126 | | - return undefined |
| 88 | + return undefined; |
127 | 89 | } |
128 | 90 | } |
129 | 91 | } |
130 | 92 |
|
131 | | -/** |
132 | | - * 注意: 对于一个change来说,同时执行的多个 operation 对应的都是同一个原始 content; |
133 | | - * 常见例子: vscode 中 cmd+d 编辑 |
134 | | - * @param content |
135 | | - * @param changes |
136 | | - */ |
137 | | - |
138 | | -export function applyChanges( |
139 | | - content: string, |
140 | | - changes: IEditorDocumentChange[], |
141 | | - eol: '\n' | '\r\n' |
142 | | -): string { |
143 | | - const textLines = new BasicTextLines(content.split(eol), eol) |
| 93 | +// TODO: eol 哪里的 |
| 94 | +export function applyChanges(content: string, changes: IEditorDocumentChange[], eol: '\n' | '\r\n'): string { |
| 95 | + const textLines = new BasicTextLines(content.split(eol), eol); |
144 | 96 | changes.forEach((change) => { |
145 | 97 | if (isEditChange(change)) { |
146 | 98 | change.changes.forEach((change) => { |
147 | | - // 这里从前端传过来的 changes 已经倒序排序过,所以可以安全的 apply |
148 | | - textLines.acceptChange(change) |
149 | | - }) |
| 99 | + textLines.acceptChange(change); |
| 100 | + }); |
150 | 101 | } else { |
151 | | - textLines.acceptEol(change.eol) |
| 102 | + textLines.acceptEol(change.eol); |
152 | 103 | } |
153 | | - }) |
154 | | - return textLines.getContent() |
155 | | -} |
156 | | - |
157 | | -function getUri(uri: string | Uri): URI { |
158 | | - const _uri = new URI(uri) |
159 | | - |
160 | | - if (!_uri.scheme) { |
161 | | - throw new Error(`没有设置 scheme: ${uri}`) |
162 | | - } |
163 | | - |
164 | | - return _uri |
| 104 | + }); |
| 105 | + return textLines.getContent(); |
165 | 106 | } |
0 commit comments