Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6acffdc

Browse files
author
Akos Kitta
committed
Fixed sketch content changes when renaming a file.
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent bd49329 commit 6acffdc

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

‎arduino-ide-extension/src/common/protocol/sketches-service-client-impl.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,17 @@ export class SketchesServiceClientImpl
9393
CurrentSketch.isValid(this._currentSketch) &&
9494
new URI(this._currentSketch.uri).isEqualOrParent(resource)
9595
) {
96-
if (type === FileChangeType.UPDATED) {
96+
// https://github.com/arduino/arduino-ide/pull/1351#pullrequestreview-1086666656
97+
// On a sketch file rename, the FS watcher will contain two changes:
98+
// - Deletion of the original file,
99+
// - Update of the new file,
100+
// Hence, `UPDATE` events must be processed but only and if only there is a `DELETED` change in the same event.
101+
// Otherwise, IDE2 would ask CLI to reload the sketch content on every save event in IDE2.
102+
if (
103+
type === FileChangeType.UPDATED &&
104+
event.changes.length === 1
105+
) {
106+
// If the event contains only one `UPDATE` change, it cannot be a rename.
97107
return;
98108
}
99109

@@ -112,8 +122,9 @@ export class SketchesServiceClientImpl
112122
return;
113123
}
114124

115-
// TODO: check if current is the same as reloaded?
116-
this.useCurrentSketch(reloadedSketch, true);
125+
if (!Sketch.sameAs(this._currentSketch, reloadedSketch)) {
126+
this.useCurrentSketch(reloadedSketch, true);
127+
}
117128
return;
118129
}
119130
// We track main sketch files changes only. // TODO: check sketch folder changes. One can rename the folder without renaming the `.ino` file.

‎arduino-ide-extension/src/common/protocol/sketches-service.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,74 @@ export namespace Sketch {
162162
const { mainFileUri, otherSketchFileUris, additionalFileUris } = sketch;
163163
return [mainFileUri, ...otherSketchFileUris, ...additionalFileUris];
164164
}
165+
const primitiveProps: Array<keyof Sketch> = ['name', 'uri', 'mainFileUri'];
166+
const arrayProps: Array<keyof Sketch> = [
167+
'additionalFileUris',
168+
'otherSketchFileUris',
169+
'rootFolderFileUris',
170+
];
171+
export function sameAs(left: Sketch, right: Sketch): boolean {
172+
for (const prop of primitiveProps) {
173+
const leftValue = left[prop];
174+
const rightValue = right[prop];
175+
assertIsNotArray(leftValue, prop, left);
176+
assertIsNotArray(rightValue, prop, right);
177+
if (leftValue !== rightValue) {
178+
return false;
179+
}
180+
}
181+
for (const prop of arrayProps) {
182+
const leftValue = left[prop];
183+
const rightValue = right[prop];
184+
assertIsArray(leftValue, prop, left);
185+
assertIsArray(rightValue, prop, right);
186+
if (leftValue.length !== rightValue.length) {
187+
return false;
188+
}
189+
}
190+
for (const prop of arrayProps) {
191+
const leftValue = left[prop];
192+
const rightValue = right[prop];
193+
assertIsArray(leftValue, prop, left);
194+
assertIsArray(rightValue, prop, right);
195+
if (
196+
toSortedString(leftValue as string[]) !==
197+
toSortedString(rightValue as string[])
198+
) {
199+
return false;
200+
}
201+
}
202+
return true;
203+
}
204+
function toSortedString(array: string[]): string {
205+
return array.slice().sort().join(',');
206+
}
207+
function assertIsNotArray(
208+
toTest: unknown,
209+
prop: keyof Sketch,
210+
object: Sketch
211+
): void {
212+
if (Array.isArray(toTest)) {
213+
throw new Error(
214+
`Expected a non-array type. Got: ${toTest}. Property was: ${prop}. Object was: ${JSON.stringify(
215+
object
216+
)}`
217+
);
218+
}
219+
}
220+
function assertIsArray(
221+
toTest: unknown,
222+
prop: keyof Sketch,
223+
object: Sketch
224+
): void {
225+
if (!Array.isArray(toTest)) {
226+
throw new Error(
227+
`Expected an array type. Got: ${toTest}. Property was: ${prop}. Object was: ${JSON.stringify(
228+
object
229+
)}`
230+
);
231+
}
232+
}
165233
}
166234

167235
export interface SketchContainer {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /