8000 #1016, #1274 Aligned the "Save your sketch" dialog behavior by kittaakos · Pull Request #1351 · arduino/arduino-ide · GitHub
[go: up one dir, main page]

Skip to content

#1016, #1274 Aligned the "Save your sketch" dialog behavior #1351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 26, 2022
Prev Previous commit
Next Next commit
Fixed sketch content changes when renaming a file.
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta committed Aug 26, 2022
commit 6acffdcb83893f640152686ed81f0ed1fb9c2eb6
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ export class SketchesServiceClientImpl
CurrentSketch.isValid(this._currentSketch) &&
new URI(this._currentSketch.uri).isEqualOrParent(resource)
) {
if (type === FileChangeType.UPDATED) {
// https://github.com/arduino/arduino-ide/pull/1351#pullrequestreview-1086666656
// On a sketch file rename, the FS watcher will contain two changes:
// - Deletion of the original file,
// - Update of the new file,
// Hence, `UPDATE` events must be processed but only and if only there is a `DELETED` change in the same event.
// Otherwise, IDE2 would ask CLI to reload the sketch content on every save event in IDE2.
if (
type === FileChangeType.UPDATED &&
event.changes.length === 1
) {
// If the event contains only one `UPDATE` change, it cannot be a rename.
return;
}

Expand All @@ -112,8 +122,9 @@ export class SketchesServiceClientImpl
return;
}

// TODO: check if current is the same as reloaded?
this.useCurrentSketch(reloadedSketch, true);
if (!Sketch.sameAs(this._currentSketch, reloadedSketch)) {
this.useCurrentSketch(reloadedSketch, true);
}
return;
}
// We track main sketch files changes only. // TODO: check sketch folder changes. One can rename the folder without renaming the `.ino` file.
Expand Down
68 changes: 68 additions & 0 deletions arduino-ide-extension/src/common/protocol/sketches-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,74 @@ export namespace Sketch {
const { mainFileUri, otherSketchFileUris, additionalFileUris } = sketch;
return [mainFileUri, ...otherSketchFileUris, ...additionalFileUris];
}
const primitiveProps: Array<keyof Sketch> = ['name', 'uri', 'mainFileUri'];
const arrayProps: Array<keyof Sketch> = [
'additionalFileUris',
'otherSketchFileUris',
'rootFolderFileUris',
];
export function sameAs(left: Sketch, right: Sketch): boolean {
for (const prop of primitiveProps) {
const leftValue = left[prop];
const rightValue = right[prop];
assertIsNotArray(leftValue, prop, left);
assertIsNotArray(rightValue, prop, right);
if (leftValue !== rightValue) {
return false;
}
}
for (const prop of arrayProps) {
const leftValue = left[prop];
const rightValue = right[prop];
assertIsArray(leftValue, prop, left);
assertIsArray(rightValue, prop, right);
if (leftValue.length !== rightValue.length) {
return false;
}
}
for (const prop of arrayProps) {
const leftValue = left[prop];
const rightValue = right[prop];
assertIsArray(leftValue, prop, left);
assertIsArray(rightValue, prop, right);
if (
toSortedString(leftValue as string[]) !==
toSortedString(rightValue as string[])
) {
return false;
}
}
return true;
}
function toSortedString(array: string[]): string {
return array.slice().sort().join(',');
}
function assertIsNotArray(
toTest: unknown,
prop: keyof Sketch,
object: Sketch
): void {
if (Array.isArray(toTest)) {
throw new Error(
`Expected a non-array type. Got: ${toTest}. Property was: ${prop}. Object was: ${JSON.stringify(
object
)}`
);
}
}
function assertIsArray(
toTest: unknown,
prop: keyof Sketch,
object: Sketch
): void {
if (!Array.isArray(toTest)) {
throw new Error(
`Expected an array type. Got: ${toTest}. Property was: ${prop}. Object was: ${JSON.stringify(
object
)}`
);
}
}
}

export interface SketchContainer {
Expand Down
0