Skip to content

Commit a46c920

Browse files
committed
replaceLine + selection
1 parent 70dfb4a commit a46c920

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

internal/fourslash/fourslash.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type FourslashTest struct {
3939
currentCaretPosition lsproto.Position
4040
lastKnownMarkerName string
4141
activeFilename string
42+
selectionEnd *lsproto.Position
4243
}
4344

4445
type scriptInfo struct {
@@ -311,7 +312,7 @@ func (f *FourslashTest) GoToEOF(t *testing.T) {
311312

312313
func (f *FourslashTest) goToPosition(t *testing.T, position lsproto.Position) {
313314
f.currentCaretPosition = position
314-
// !!! clean up selection
315+
f.selectionEnd = nil
315316
}
316317

317318
func (f *FourslashTest) Markers() []*Marker {
@@ -648,15 +649,55 @@ func (f *FourslashTest) Paste(t *testing.T, text string) {
648649
script := f.getScriptInfo(f.activeFilename)
649650
start := int(f.converters.LineAndCharacterToPosition(script, f.currentCaretPosition))
650651
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?
652693
}
653694

654695
// Inserts the text currently at the caret position character by character, as if the user typed it.
655696
func (f *FourslashTest) typeText(t *testing.T, text string) {
656697
script := f.getScriptInfo(f.activeFilename)
657698
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(), "")
660701

661702
totalSize := 0
662703

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestBasicReplaceLine(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
const content = `export {};
14+
interface Point {
15+
x: number;
16+
y: number;
17+
}
18+
declare const p: Point;
19+
p./*a*/`
20+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
21+
f.VerifyCompletions(t, "a", &fourslash.CompletionsExpectedList{
22+
IsIncomplete: false,
23+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
24+
CommitCharacters: &defaultCommitCharacters,
25+
},
26+
Items: &fourslash.CompletionsExpectedItems{
27+
Includes: []fourslash.CompletionsExpectedItem{"y"},
28+
},
29+
})
30+
f.ReplaceLine(t, 3, " z: number;") // `y: number;`
31+
f.VerifyCompletions(t, "a", &fourslash.CompletionsExpectedList{
32+
IsIncomplete: false,
33+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
34+
CommitCharacters: &defaultCommitCharacters,
35+
},
36+
Items: &fourslash.CompletionsExpectedItems{
37+
Excludes: []string{"y"},
38+
Includes: []fourslash.CompletionsExpectedItem{"z"},
39+
},
40+
})
41+
}

0 commit comments

Comments
 (0)