Skip to content

Commit

Permalink
Preserve order of cells when inserting via code block button tray
Browse files Browse the repository at this point in the history
  • Loading branch information
rhennigan committed Jan 8, 2024
1 parent 555a987 commit 91330db
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions Source/Chatbook/Formatting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ $externalLanguageRules = Replace[
$$mdRow = Except[ "\n" ].. ~~ Repeated[ ("|" ~~ Except[ "\n" ]..), { 2, Infinity } ] ~~ ("\n"|EndOfString);
$$mdTable = $$mdRow ~~ $$mdRow ..;

$chatGeneratedCellTag = "ChatGeneratedCell";

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Chat Output Formatting*)
Expand Down Expand Up @@ -444,8 +446,7 @@ insertCodeBelow[ cell_Cell, evaluate_ ] :=
Module[ { cellObj, nbo },
cellObj = topParentCell @ EvaluationCell[ ];
nbo = parentNotebook @ cellObj;
SelectionMove[ cellObj, After, Cell ];
NotebookWrite[ nbo, stripMarkdownBoxes @ cell, All ];
insertAfterChatGeneratedCells[ cellObj, cell ];
If[ TrueQ @ evaluate,
selectionEvaluateCreateCell @ nbo,
SelectionMove[ nbo, After, CellContents ]
Expand All @@ -457,6 +458,35 @@ insertCodeBelow[ string_String, evaluate_ ] :=

insertCodeBelow // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*insertAfterChatGeneratedCells*)
insertAfterChatGeneratedCells // beginDefinition;

insertAfterChatGeneratedCells[ cellObj_CellObject, cell_Cell ] := Enclose[
Module[ { nbo, allCells, cellsAfter, tagged, inserted, insertionPoint },

nbo = ConfirmMatch[ parentNotebook @ cellObj, _NotebookObject, "ParentNotebook" ];
allCells = ConfirmMatch[ Cells @ nbo, { __CellObject }, "AllCells" ];
cellsAfter = Replace[ allCells, { { ___, cellObj, after___ } :> { after }, _ :> { } } ];

tagged = ConfirmBy[
AssociationThread[ cellsAfter -> Flatten @* List /@ CurrentValue[ cellsAfter, CellTags ] ],
AssociationQ,
"Tagged"
];

inserted = ConfirmBy[ TakeWhile[ tagged, MemberQ[ $chatGeneratedCellTag ] ], AssociationQ, "Inserted" ];
insertionPoint = ConfirmMatch[ Last[ Keys @ inserted, cellObj ], _CellObject, "InsertionPoint" ];

SelectionMove[ insertionPoint, After, Cell ];
NotebookWrite[ nbo, preprocessInsertedCell @ cell, All ];
],
throwInternalFailure
];

insertAfterChatGeneratedCells // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*copyCode*)
Expand All @@ -465,6 +495,29 @@ copyCode[ cell_CellObject ] := copyCode @ getCodeBlockContent @ cell;
copyCode[ code: _Cell|_String ] := CopyToClipboard @ stripMarkdownBoxes @ code;
copyCode // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*preprocessInsertedCell*)
preprocessInsertedCell // beginDefinition;
preprocessInsertedCell[ cell_ ] := addInsertedCellTags @ stripMarkdownBoxes @ cell;
preprocessInsertedCell // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*addInsertedCellTags*)
addInsertedCellTags // beginDefinition;

addInsertedCellTags[ Cell[ a__, CellTags -> tag_String, b___ ] ] :=
addInsertedCellTags @ Cell[ a, CellTags -> { tag }, b ];

addInsertedCellTags[ Cell[ a__, CellTags -> { tags___String }, b___ ] ] :=
Cell[ a, CellTags -> DeleteDuplicates @ { $chatGeneratedCellTag, tags }, b ];

addInsertedCellTags[ Cell[ a: Except[ CellTags -> _ ].. ] ] :=
Cell[ a, CellTags -> { $chatGeneratedCellTag } ];

addInsertedCellTags // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*stripMarkdownBoxes*)
Expand Down

0 comments on commit 91330db

Please sign in to comment.