Skip to content
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

[workspace] show rich difference info on save #17

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/python/workspace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING

from .diff import diff

if TYPE_CHECKING:
from ...lib.pyodide.start.loader import setup_module
else:
Expand Down Expand Up @@ -47,17 +49,19 @@ def save(self, path: str, content: str, reload=True):
if reload:
unload(path2module(path))

if __debug__:
from difflib import ndiff
differences = diff(Path(self.directory.name, path).read_text(), content)

if __debug__:
from js import console

console.group(f"Unloaded {path2module(path)}")
console.log("\n".join(ndiff(Path(self.directory.name, path).read_text().splitlines(), content.splitlines())))
console.log(differences)
console.groupEnd()

file.write_text(content)

return f"```diff\n{differences}\n```\n"


def path2module(filename: str):
return filename.removesuffix(".py").replace("/", ".").replace(".__init__", "")
Expand Down
5 changes: 5 additions & 0 deletions src/python/workspace/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from difflib import ndiff


def diff(a: str, b: str):
return "\n".join(i for i in ndiff(a.splitlines(), b.splitlines()) if not i.startswith(" "))
2 changes: 1 addition & 1 deletion src/python/workspace/workspace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { PyProxy } from "pyodide/ffi";
export class WorkspaceAPI extends PyProxy {
close(): void;
sync(sources: Record<string, string>, reload = true): void;
save(path: string, content: string, reload = true): void;
save(path: string, content: string, reload = true): string | void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Clarify the return type of save method.

The save method now returns string | void. It would be helpful to document under what conditions each type is returned to avoid confusion.

Suggested change
save(path: string, content: string, reload = true): string | void;
/**
* Saves the content to the specified path.
*
* @param path - The path where the content should be saved.
* @param content - The content to save.
* @param reload - Whether to reload after saving. Defaults to true.
* @returns A string message if an error occurs, otherwise void.
*/
save(path: string, content: string, reload = true): string | void;

}
6 changes: 4 additions & 2 deletions src/routes/(workspace)/Workspace.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
<SetupWorkspace {sources} let:save>
<Pane defaultSize={70} minSize={10} class="relative">
<FileContent on:save={({ detail: content }) => {
save(focusedFile, content.replaceAll("\r\n", "\n"));
toastMarkdown(`\`${focusedFile}\` saved`);
const diff = save(focusedFile, content.replaceAll("\r\n", "\n"));
diff
? toastMarkdown(`\`${focusedFile}\` saved\n\n${diff}`)
: toastMarkdown(`\`${focusedFile}\` is up to date`);
}} bind:content={sources[focusedFile]} lang={focusedFile.slice(focusedFile.lastIndexOf(".") + 1)} />
</Pane>
<PaneResizer class="group">
Expand Down
Loading