Skip to content

Commit b04c8a0

Browse files
authored
Fixing range for primary edit (#59369)
1 parent 8a0e47e commit b04c8a0

File tree

3 files changed

+367
-2
lines changed

3 files changed

+367
-2
lines changed

src/server/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,15 +2319,15 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
23192319
runWithTemporaryFileUpdate(rootFile: string, updatedText: string, cb: (updatedProgram: Program, originalProgram: Program | undefined, updatedFile: SourceFile) => void) {
23202320
const originalProgram = this.program;
23212321
const rootSourceFile = Debug.checkDefined(this.program?.getSourceFile(rootFile), "Expected file to be part of program");
2322-
const originalText = Debug.checkDefined(rootSourceFile.getText());
2322+
const originalText = Debug.checkDefined(rootSourceFile.getFullText());
23232323

23242324
this.getScriptInfo(rootFile)?.editContent(0, originalText.length, updatedText);
23252325
this.updateGraph();
23262326
try {
23272327
cb(this.program!, originalProgram, (this.program?.getSourceFile(rootFile))!);
23282328
}
23292329
finally {
2330-
this.getScriptInfo(rootFile)?.editContent(0, this.program!.getSourceFile(rootFile)!.getText().length, originalText);
2330+
this.getScriptInfo(rootFile)?.editContent(0, updatedText.length, originalText);
23312331
}
23322332
}
23332333

Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
currentDirectory:: / useCaseSensitiveFileNames: false
2+
Info seq [hh:mm:ss:mss] Provided types map file "/typesMap.json" doesn't exist
3+
//// [/a.ts]
4+
5+
6+
7+
8+
9+
10+
//// [/b.ts]
11+
export interface Foo { }
12+
export const a = 1;
13+
export const t = 1;
14+
15+
export const foo: Foo = { };
16+
export const k = a+ t;
17+
18+
//// [/lib.d.ts]
19+
lib.d.ts-Text
20+
21+
//// [/lib.decorators.d.ts]
22+
lib.decorators.d.ts-Text
23+
24+
//// [/lib.decorators.legacy.d.ts]
25+
lib.decorators.legacy.d.ts-Text
26+
27+
//// [/tsconfig.json]
28+
{ "files": ["a.ts", "b.ts"] }
29+
30+
31+
Info seq [hh:mm:ss:mss] request:
32+
{
33+
"seq": 0,
34+
"type": "request",
35+
"arguments": {
36+
"file": "/a.ts"
37+
},
38+
"command": "open"
39+
}
40+
Info seq [hh:mm:ss:mss] getConfigFileNameForFile:: File: /a.ts ProjectRootPath: undefined:: Result: /tsconfig.json
41+
Info seq [hh:mm:ss:mss] Creating configuration project /tsconfig.json
42+
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /tsconfig.json 2000 undefined Project: /tsconfig.json WatchType: Config file
43+
Info seq [hh:mm:ss:mss] event:
44+
{
45+
"seq": 0,
46+
"type": "event",
47+
"event": "projectLoadingStart",
48+
"body": {
49+
"projectName": "/tsconfig.json",
50+
"reason": "Creating possible configured project for /a.ts to open"
51+
}
52+
}
53+
Info seq [hh:mm:ss:mss] Config: /tsconfig.json : {
54+
"rootNames": [
55+
"/a.ts",
56+
"/b.ts"
57+
],
58+
"options": {
59+
"configFilePath": "/tsconfig.json"
60+
}
61+
}
62+
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /b.ts 500 undefined WatchType: Closed Script info
63+
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /tsconfig.json
64+
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /lib.d.ts 500 undefined WatchType: Closed Script info
65+
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /lib.decorators.d.ts 500 undefined WatchType: Closed Script info
66+
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /lib.decorators.legacy.d.ts 500 undefined WatchType: Closed Script info
67+
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /tsconfig.json projectStateVersion: 1 projectProgramVersion: 0 structureChanged: true structureIsReused:: Not Elapsed:: *ms
68+
Info seq [hh:mm:ss:mss] Project '/tsconfig.json' (Configured)
69+
Info seq [hh:mm:ss:mss] Files (5)
70+
/lib.d.ts Text-1 lib.d.ts-Text
71+
/lib.decorators.d.ts Text-1 lib.decorators.d.ts-Text
72+
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text
73+
/a.ts SVC-1-0 "\n\n\n\n"
74+
/b.ts Text-1 "export interface Foo { }\nexport const a = 1;\nexport const t = 1;\n\nexport const foo: Foo = { };\nexport const k = a+ t;"
75+
76+
77+
lib.d.ts
78+
Default library for target 'es5'
79+
lib.decorators.d.ts
80+
Library referenced via 'decorators' from file 'lib.d.ts'
81+
lib.decorators.legacy.d.ts
82+
Library referenced via 'decorators.legacy' from file 'lib.d.ts'
83+
a.ts
84+
Part of 'files' list in tsconfig.json
85+
b.ts
86+
Part of 'files' list in tsconfig.json
87+
88+
Info seq [hh:mm:ss:mss] -----------------------------------------------
89+
Info seq [hh:mm:ss:mss] event:
90+
{
91+
"seq": 0,
92+
"type": "event",
93+
"event": "projectLoadingFinish",
94+
"body": {
95+
"projectName": "/tsconfig.json"
96+
}
97+
}
98+
Info seq [hh:mm:ss:mss] event:
99+
{
100+
"seq": 0,
101+
"type": "event",
102+
"event": "configFileDiag",
103+
"body": {
104+
"triggerFile": "/a.ts",
105+
"configFile": "/tsconfig.json",
106+
"diagnostics": []
107+
}
108+
}
109+
Info seq [hh:mm:ss:mss] Project '/tsconfig.json' (Configured)
110+
Info seq [hh:mm:ss:mss] Files (5)
111+
112+
Info seq [hh:mm:ss:mss] -----------------------------------------------
113+
Info seq [hh:mm:ss:mss] Open files:
114+
Info seq [hh:mm:ss:mss] FileName: /a.ts ProjectRootPath: undefined
115+
Info seq [hh:mm:ss:mss] Projects: /tsconfig.json
116+
Info seq [hh:mm:ss:mss] response:
117+
{
118+
"seq": 0,
119+
"type": "response",
120+
"command": "open",
121+
"request_seq": 0,
122+
"success": true,
123+
"performanceData": {
124+
"updateGraphDurationMs": *
125+
}
126+
}
127+
After Request
128+
watchedFiles::
129+
/b.ts: *new*
130+
{"pollingInterval":500}
131+
/lib.d.ts: *new*
132+
{"pollingInterval":500}
133+
/lib.decorators.d.ts: *new*
134+
{"pollingInterval":500}
135+
/lib.decorators.legacy.d.ts: *new*
136+
{"pollingInterval":500}
137+
/tsconfig.json: *new*
138+
{"pollingInterval":2000}
139+
140+
Projects::
141+
/tsconfig.json (Configured) *new*
142+
projectStateVersion: 1
143+
projectProgramVersion: 1
144+
145+
ScriptInfos::
146+
/a.ts (Open) *new*
147+
version: SVC-1-0
148+
containingProjects: 1
149+
/tsconfig.json *default*
150+
/b.ts *new*
151+
version: Text-1
152+
containingProjects: 1
153+
/tsconfig.json
154+
/lib.d.ts *new*
155+
version: Text-1
156+
containingProjects: 1
157+
/tsconfig.json
158+
/lib.decorators.d.ts *new*
159+
version: Text-1
160+
containingProjects: 1
161+
/tsconfig.json
162+
/lib.decorators.legacy.d.ts *new*
163+
version: Text-1
164+
containingProjects: 1
165+
/tsconfig.json
166+
167+
Info seq [hh:mm:ss:mss] request:
168+
{
169+
"seq": 1,
170+
"type": "request",
171+
"arguments": {
172+
"formatOptions": {
173+
"indentSize": 4,
174+
"tabSize": 4,
175+
"newLineCharacter": "\n",
176+
"convertTabsToSpaces": true,
177+
"indentStyle": 2,
178+
"insertSpaceAfterConstructor": false,
179+
"insertSpaceAfterCommaDelimiter": true,
180+
"insertSpaceAfterSemicolonInForStatements": true,
181+
"insertSpaceBeforeAndAfterBinaryOperators": true,
182+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
183+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
184+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
185+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
186+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
187+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
188+
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
189+
"insertSpaceBeforeFunctionParenthesis": false,
190+
"placeOpenBraceOnNewLineForFunctions": false,
191+
"placeOpenBraceOnNewLineForControlBlocks": false,
192+
"semicolons": "ignore",
193+
"trimTrailingWhitespace": true,
194+
"indentSwitchCase": true
195+
}
196+
},
197+
"command": "configure"
198+
}
199+
Info seq [hh:mm:ss:mss] Format host information updated
200+
Info seq [hh:mm:ss:mss] response:
201+
{
202+
"seq": 0,
203+
"type": "response",
204+
"command": "configure",
205+
"request_seq": 1,
206+
"success": true
207+
}
208+
Info seq [hh:mm:ss:mss] request:
209+
{
210+
"seq": 2,
211+
"type": "request",
212+
"arguments": {
213+
"file": "/a.ts",
214+
"pastedText": [
215+
"export const foo: Foo = {};"
216+
],
217+
"pasteLocations": [
218+
{
219+
"start": {
220+
"line": 4,
221+
"offset": 1
222+
},
223+
"end": {
224+
"line": 4,
225+
"offset": 1
226+
}
227+
}
228+
],
229+
"copiedFrom": {
230+
"file": "b.ts",
231+
"spans": [
232+
{
233+
"start": {
234+
"line": 5,
235+
"offset": 1
236+
},
237+
"end": {
238+
"line": 5,
239+
"offset": 29
240+
}
241+
}
242+
]
243+
}
244+
},
245+
"command": "getPasteEdits"
246+
}
247+
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /tsconfig.json
248+
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /tsconfig.json projectStateVersion: 2 projectProgramVersion: 1 structureChanged: false structureIsReused:: Completely Elapsed:: *ms
249+
Info seq [hh:mm:ss:mss] Project '/tsconfig.json' (Configured)
250+
Info seq [hh:mm:ss:mss] Files (5)
251+
/lib.d.ts Text-1 lib.d.ts-Text
252+
/lib.decorators.d.ts Text-1 lib.decorators.d.ts-Text
253+
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text
254+
/a.ts SVC-1-1 "\n\n\nexport const foo: Foo = {};\n"
255+
/b.ts Text-1 "export interface Foo { }\nexport const a = 1;\nexport const t = 1;\n\nexport const foo: Foo = { };\nexport const k = a+ t;"
256+
257+
Info seq [hh:mm:ss:mss] -----------------------------------------------
258+
Info seq [hh:mm:ss:mss] response:
259+
{
260+
"seq": 0,
261+
"type": "response",
262+
"command": "getPasteEdits",
263+
"request_seq": 2,
264+
"success": true,
265+
"performanceData": {
266+
"updateGraphDurationMs": *
267+
},
268+
"body": {
269+
"edits": [
270+
{
271+
"fileName": "/a.ts",
272+
"textChanges": [
273+
{
274+
"start": {
275+
"line": 1,
276+
"offset": 1
277+
},
278+
"end": {
279+
"line": 1,
280+
"offset": 1
281+
},
282+
"newText": "import { Foo } from \"./b\";\n"
283+
},
284+
{
285+
"start": {
286+
"line": 4,
287+
"offset": 1
288+
},
289+
"end": {
290+
"line": 4,
291+
"offset": 1
292+
},
293+
"newText": "export const foo: Foo = {};"
294+
}
295+
]
296+
}
297+
],
298+
"fixId": "providePostPasteEdits"
299+
}
300+
}
301+
After Request
302+
Projects::
303+
/tsconfig.json (Configured) *changed*
304+
projectStateVersion: 3 *changed*
305+
projectProgramVersion: 1
306+
dirty: true *changed*
307+
308+
ScriptInfos::
309+
/a.ts (Open) *changed*
310+
version: SVC-1-2 *changed*
311+
containingProjects: 1
312+
/tsconfig.json *default*
313+
/b.ts
314+
version: Text-1
315+
containingProjects: 1
316+
/tsconfig.json
317+
/lib.d.ts
318+
version: Text-1
319+
containingProjects: 1
320+
/tsconfig.json
321+
/lib.decorators.d.ts
322+
version: Text-1
323+
containingProjects: 1
324+
/tsconfig.json
325+
/lib.decorators.legacy.d.ts
326+
version: Text-1
327+
containingProjects: 1
328+
/tsconfig.json
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////
5+
////
6+
////
7+
//// [||]
8+
////
9+
10+
// @Filename: /b.ts
11+
//// export interface Foo { }
12+
//// export const a = 1;
13+
//// export const t = 1;
14+
////
15+
//// [|export const foo: Foo = { };|]
16+
//// [|export const k = a+ t;|]
17+
18+
// @Filename: /tsconfig.json
19+
////{ "files": ["a.ts", "b.ts"] }
20+
21+
const range = test.ranges();
22+
verify.pasteEdits({
23+
args: {
24+
pastedText: [`export const foo: Foo = {};`],
25+
pasteLocations: [range[0]],
26+
copiedFrom: { file: "b.ts", range: [range[1]] },
27+
},
28+
newFileContents: {
29+
"/a.ts":
30+
`import { Foo } from "./b";
31+
32+
33+
34+
export const foo: Foo = {};
35+
`
36+
}
37+
});

0 commit comments

Comments
 (0)