@@ -39,6 +39,7 @@ type FourslashTest struct {
39
39
currentCaretPosition lsproto.Position
40
40
lastKnownMarkerName string
41
41
activeFilename string
42
+ selectionEnd * lsproto.Position
42
43
}
43
44
44
45
type scriptInfo struct {
@@ -311,7 +312,7 @@ func (f *FourslashTest) GoToEOF(t *testing.T) {
311
312
312
313
func (f * FourslashTest ) goToPosition (t * testing.T , position lsproto.Position ) {
313
314
f .currentCaretPosition = position
314
- // !!! clean up selection
315
+ f . selectionEnd = nil
315
316
}
316
317
317
318
func (f * FourslashTest ) Markers () []* Marker {
@@ -648,15 +649,55 @@ func (f *FourslashTest) Paste(t *testing.T, text string) {
648
649
script := f .getScriptInfo (f .activeFilename )
649
650
start := int (f .converters .LineAndCharacterToPosition (script , f .currentCaretPosition ))
650
651
f .editScriptAndUpdateMarkers (t , f .activeFilename , start , start , text )
651
- // this.checkPostEditInvariants();
652
+ // this.checkPostEditInvariants(); // !!! do we need this?
653
+ }
654
+
655
+ // Selects a line and replaces it with a new text.
656
+ func (f * FourslashTest ) ReplaceLine (t * testing.T , lineIndex int , text string ) {
657
+ f .selectLine (t , lineIndex )
658
+ f .typeText (t , text )
659
+ }
660
+
661
+ func (f * FourslashTest ) selectLine (t * testing.T , lineIndex int ) {
662
+ script := f .getScriptInfo (f .activeFilename )
663
+ start := script .lineMap .LineStarts [lineIndex ]
664
+ end := script .lineMap .LineStarts [lineIndex + 1 ] - 1
665
+ f .selectRange (t , core .NewTextRange (int (start ), int (end )))
666
+ }
667
+
668
+ func (f * FourslashTest ) selectRange (t * testing.T , textRange core.TextRange ) {
669
+ script := f .getScriptInfo (f .activeFilename )
670
+ start := f .converters .PositionToLineAndCharacter (script , core .TextPos (textRange .Pos ()))
671
+ end := f .converters .PositionToLineAndCharacter (script , core .TextPos (textRange .End ()))
672
+ f .goToPosition (t , start )
673
+ f .selectionEnd = & end
674
+ }
675
+
676
+ func (f * FourslashTest ) getSelection () core.TextRange {
677
+ script := f .getScriptInfo (f .activeFilename )
678
+ if f .selectionEnd == nil {
679
+ return core .NewTextRange (
680
+ int (f .converters .LineAndCharacterToPosition (script , f .currentCaretPosition )),
681
+ int (f .converters .LineAndCharacterToPosition (script , f .currentCaretPosition )),
682
+ )
683
+ }
684
+ return core .NewTextRange (
685
+ int (f .converters .LineAndCharacterToPosition (script , f .currentCaretPosition )),
686
+ int (f .converters .LineAndCharacterToPosition (script , * f .selectionEnd )),
687
+ )
688
+ }
689
+
690
+ func (f * FourslashTest ) Replace (t * testing.T , start int , length int , text string ) {
691
+ f .editScriptAndUpdateMarkers (t , f .activeFilename , start , start + length , text )
692
+ // f.checkPostEditInvariants() // !!! do we need this?
652
693
}
653
694
654
695
// Inserts the text currently at the caret position character by character, as if the user typed it.
655
696
func (f * FourslashTest ) typeText (t * testing.T , text string ) {
656
697
script := f .getScriptInfo (f .activeFilename )
657
698
offset := int (f .converters .LineAndCharacterToPosition (script , f .currentCaretPosition ))
658
- // selection := f.getSelection() // !!! selection
659
- // this.replace( selection.pos , selection.end - selection.pos , ""); // !!! selection
699
+ selection := f .getSelection ()
700
+ f . Replace ( t , selection .Pos () , selection .End () - selection .Pos () , "" )
660
701
661
702
totalSize := 0
662
703
0 commit comments