-
Notifications
You must be signed in to change notification settings - Fork 448
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
improve: Move highlighted code to newly created cells #2484
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice QOL improvement! One thing — after splitting, both cells should be marked as stale
@akshayka Good catch thanks! |
@wasimsandhu - can you verify this works with SQL and markdown? We probably have to do something similar to |
frontend/src/core/cells/cells.ts
Outdated
@@ -214,17 +216,24 @@ const { | |||
: state.cellIds.topLevelIds.indexOf(cellId); | |||
const insertionIndex = before ? index : index + 1; | |||
|
|||
let cellContents = code; | |||
const cellEditorView = getCellEditorView(cellId as CellId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we should pass a param to createNewCell
whether we should includeSelectionAsInitialCode
.
we run createNewCell
in many places and this would be weird for most cases (e.g. snippets, data-source panel, AI cell, etc).
could we also assert code
and includeSelectionAsInitialCode
are never both true? there is a typescript way to do this:
- code?: string;
+ code?: string | {type: 'from-selection', cellId: CellId}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edit: Implemented includeSelectionAsInitialCode
, but not sure I understand the type for code
When editing a SQL/markdown cell and triggering a create new cell above/below (via button or shortcut), a Python cell is created. Do we want to change this behavior so that a cell of the same type is created? (This makes sense to me.) |
This might take some getting used to. I would keep it python for new cells, but keep the same language for "yanked" cells. thoughts? is that weird ux? |
frontend/src/core/cells/cells.ts
Outdated
const cellEditorView = getCellEditorView(id); | ||
if (cellEditorView) { | ||
const [highlighted, leftover] = extractHighlightedCode(cellEditorView); | ||
cellContents = highlighted; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should only do this this if highlighted
is not empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a couple test cases for this?
createCell
whenincludeSelectionAsInitialCode
is true and there is selectioncreateCell
whenincludeSelectionAsInitialCode
is false and there is selectioncreateCell
whenincludeSelectionAsInitialCode
is true and there is no selectioncreateCell
whenincludeSelectionAsInitialCode
is false and there is no selection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it will catch small bugs like state.cellData[id].edited=true
being set incorrectly.
also, doing state.cellData[id].edited
is mutable, we will need to be immutable when using react
.
state.cellData = {
...state.cellData,
[id] : {
...state.cellData[id],
code: ...,
edited: ...
}
}
Works for me! |
@@ -1276,4 +1278,130 @@ describe("cell reducer", () => { | |||
const cell = cells[0]; | |||
expect(cell.consoleOutputs).toEqual([STDOUT1, STDOUT2]); | |||
}); | |||
|
|||
it("can create a cell using selected code", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put each of these in there own tests and share setup code?
describe("can create a cell using selected code"
beforeEach(() => {
// setup code
})
it("can create when includeSelectionAsInitialCode is true and there is selection", () => {
...test one
})
...
});
this makes it easier to follow given the same initial setup.
could you also:
- verify edited state
- do a test with markdown
includeSelectionAsInitialCode
- i noticed a bug when i add a cell above/below a markdown cell withincludeSelectionAsInitialCode=true
, it says it is edited
[id]: { | ||
...state.cellData[id], | ||
code: leftover, | ||
edited: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might need to be edited: Boolean(leftover) && state.cellData[id].code !== state.cellData[id].code
,
i noticed a bug with markdown with no selection being made stale, it would be great to prove this in a test: get the test to fail first and then fix this code so the tests passes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made a suggestion above that you may not need this.
expect(highlighted).toEqual(thirdLine); | ||
expect(leftover).toEqual(`${firstLine}\n${secondLine}`); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you write some tests for markdown and sql?
- sql x no selection
- sql x with selection
- markdown x no selection
- markdown x with selection
frontend/src/core/cells/cells.ts
Outdated
const editorView = state.cellHandles[id].current?.editorView; | ||
if (editorView) { | ||
const [highlighted, leftover] = extractHighlightedCode(editorView); | ||
if (highlighted.length > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the issue is that for markdown/sql, highlighted > 0. if you write a test for extractHighlightedCode
, can we get verify that no selection -> no highlighted
|
||
export function extractHighlightedCode(editor: EditorView) { | ||
const code = editor.state.doc.toString(); | ||
let { from, to } = editor.state.selection.main; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could for if (from === to) {return ["", code]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or could do if (from === to) {return null
to signify nothing was highlighted. it will force consumers of this downstream to check null
.
i think that is better / clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented this and it seems to have fixed the stale issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome work!
🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.9.2-dev17 |
This reverts commit 25afc55. After some experimentation we decided that moving highlighted code to newly created cells is not intuitive. It's common to have code highlighted without an intention to split the cell. Splitting is destructive because it marks cells as stale, so it should be something that is done explicitly by the user -- either by the split hotkey or by simply copy pasting.
📝 Summary
Implements #2474.
🔍 Description of Changes
When a new cell is created, any highlighted code is cut from the original cell and pasted into the new cell.
📋 Checklist
📜 Reviewers
@akshayka OR @mscolnick