@@ -6,7 +6,7 @@ import { EditorState, TextSelection } from "@tiptap/pm/state";
66import { splitBlockKeepMarks } from "@tiptap/pm/commands" ;
77import { describe , expect , it } from "vite-plus/test" ;
88
9- import { collapseExpandedComposerCursor } from "./composer-logic" ;
9+ import { collapseExpandedComposerCursor , pastedPathQueryLength } from "./composer-logic" ;
1010
1111import {
1212 buildDocJson ,
@@ -942,6 +942,94 @@ describe("pasting into a list item or quote", () => {
942942 } ) ;
943943} ) ;
944944
945+ describe ( "pasting into a path query" , ( ) => {
946+ const skillLabelFor = ( name : string ) => ( { label : name , description : null } ) ;
947+
948+ // Inserts `pasted` at the end of a one-line prompt the way the composer's
949+ // paste handler does. `prompt` may be a document, to place the caret after a chip.
950+ function paste ( prompt : string | ProseMirrorNode , pasted : string , pathQueryActive = true ) {
951+ const promptDoc =
952+ typeof prompt === "string"
953+ ? ProseMirrorNode . fromJSON ( schema , buildDocJson ( prompt , skillLabelFor ) )
954+ : prompt ;
955+ const line = promptDoc . lastChild ! ;
956+ const lineBefore = line . textBetween ( 0 , line . content . size , undefined , "\uFFFC" ) ;
957+ const literalLength = pastedPathQueryLength ( lineBefore , pasted , pathQueryActive ) ;
958+ const blocks = buildTiptapContent ( pasted , skillLabelFor , { literalLength } ) ;
959+ const [ first , ...rest ] = blocks . map ( ( block ) => ProseMirrorNode . fromJSON ( schema , block ) ) ;
960+ const head = promptDoc . lastChild ! . content . append ( first ! . content ) ;
961+ const doc = schema . node ( "doc" , null , [ first ! . copy ( head ) , ...rest ] ) ;
962+ doc . check ( ) ;
963+ return doc ;
964+ }
965+
966+ function boldText ( doc : ProseMirrorNode ) {
967+ const bold : string [ ] = [ ] ;
968+ doc . descendants ( ( node ) => {
969+ if ( node . isText && node . marks . some ( ( mark ) => mark . type . name === "bold" ) ) {
970+ bold . push ( node . text ! ) ;
971+ }
972+ } ) ;
973+ return bold ;
974+ }
975+
976+ it . each ( [ "src/__test__.tsx" , "src/__tests__/a.ts" , "pkg/__init__.py" , "a/**b**/c" ] ) (
977+ "keeps %s literal after @ so file search can match it" ,
978+ ( path ) => {
979+ const doc = paste ( "Open @" , path ) ;
980+ expect ( boldText ( doc ) ) . toEqual ( [ ] ) ;
981+ expect ( serializeEditorDoc ( doc ) . value ) . toBe ( `Open @${ path } ` ) ;
982+ } ,
983+ ) ;
984+
985+ it ( "still styles the text after the path" , ( ) => {
986+ const doc = paste ( "Open @" , "src/__test__.tsx and **this**\n- [ ] then __that__" ) ;
987+ expect ( boldText ( doc ) ) . toEqual ( [ "this" , "that" ] ) ;
988+ expect ( doc . lastChild ! . type . name ) . toBe ( "taskList" ) ;
989+ expect ( serializeEditorDoc ( doc ) . value ) . toBe (
990+ "Open @src/__test__.tsx and **this**\n- [ ] then **that**" ,
991+ ) ;
992+ } ) ;
993+
994+ it ( "parses markdown after Escape dismisses the path query" , ( ) => {
995+ const doc = paste ( "Open @" , "__hello__ world" , false ) ;
996+ expect ( boldText ( doc ) ) . toEqual ( [ "hello" ] ) ;
997+ expect ( serializeEditorDoc ( doc ) . value ) . toBe ( "Open @**hello** world" ) ;
998+ } ) ;
999+
1000+ it ( "keeps the rest of the path's line on that line" , ( ) => {
1001+ const doc = paste ( "Open @" , "src/a.ts - not a list item" ) ;
1002+ expect ( doc . childCount ) . toBe ( 1 ) ;
1003+ expect ( serializeEditorDoc ( doc ) . value ) . toBe ( "Open @src/a.ts - not a list item" ) ;
1004+ } ) ;
1005+
1006+ it ( "parses markdown when the paste does not continue a path query" , ( ) => {
1007+ expect ( boldText ( paste ( "Open " , "src/__test__.tsx" ) ) ) . toEqual ( [ "test" ] ) ;
1008+ expect ( boldText ( paste ( "Open @a " , "__b__" ) ) ) . toEqual ( [ "b" ] ) ;
1009+ expect ( boldText ( paste ( "Open @" , " __b__" ) ) ) . toEqual ( [ "b" ] ) ;
1010+ } ) ;
1011+
1012+ it ( "keeps a path literal after a partly styled query" , ( ) => {
1013+ const doc = paste ( "Open @**src/**" , "__test__.tsx" ) ;
1014+ expect ( boldText ( doc ) ) . toEqual ( [ "src/" ] ) ;
1015+ expect ( serializeEditorDoc ( doc ) . value ) . toBe ( "Open @**src/**__test__.tsx" ) ;
1016+ } ) ;
1017+
1018+ it ( "parses markdown pasted right after a mention chip" , ( ) => {
1019+ const withSpace = ProseMirrorNode . fromJSON (
1020+ schema ,
1021+ buildDocJson ( "Open @README.md " , skillLabelFor ) ,
1022+ ) ;
1023+ const paragraph = withSpace . firstChild ! ;
1024+ expect ( paragraph . lastChild ! . text ) . toBe ( " " ) ;
1025+ const chipLast = schema . node ( "doc" , null , [
1026+ paragraph . copy ( paragraph . content . cut ( 0 , paragraph . content . size - 1 ) ) ,
1027+ ] ) ;
1028+ expect ( chipLast . firstChild ! . lastChild ! . type . name ) . toBe ( "composer-mention" ) ;
1029+ expect ( boldText ( paste ( chipLast , "__b__" ) ) ) . toEqual ( [ "b" ] ) ;
1030+ } ) ;
1031+ } ) ;
1032+
9451033describe ( "caret stops at styled edges" , ( ) => {
9461034 function stateAt ( value : string , pmPos : number ) {
9471035 const doc = ProseMirrorNode . fromJSON (
0 commit comments