-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b7fd98
commit 118bdcd
Showing
12 changed files
with
323 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/creachadair/jrpc2" | ||
"github.com/sourcegraph/go-lsp" | ||
lsctx "pkg.nimblebun.works/tsc-language-server/langserver/context" | ||
"pkg.nimblebun.works/tsc-language-server/langserver/filesystem/filehandler" | ||
"pkg.nimblebun.works/tsc-language-server/tsc" | ||
) | ||
|
||
// FoldingRangeParams is a (loosely reproduced) structure that contains the | ||
// fields sent on a textDocument/foldingRange request. | ||
type FoldingRangeParams struct { | ||
TextDocument lsp.TextDocumentIdentifier `json:"textDocument"` | ||
} | ||
|
||
// TextDocumentFoldingRange is the callback that runs on the | ||
// "textDocument/foldingRange" method. | ||
func (mh *MethodHandler) TextDocumentFoldingRange(ctx context.Context, req *jrpc2.Request) ([]tsc.FoldingRange, error) { | ||
var params FoldingRangeParams | ||
err := req.UnmarshalParams(jrpc2.NonStrict(¶ms)) | ||
if err != nil { | ||
return []tsc.FoldingRange{}, err | ||
} | ||
|
||
fs, err := lsctx.FileSystem(ctx) | ||
if err != nil { | ||
return []tsc.FoldingRange{}, err | ||
} | ||
|
||
handler := filehandler.FromDocumentURI(params.TextDocument.URI) | ||
path, err := handler.FullPath() | ||
if err != nil { | ||
return []tsc.FoldingRange{}, err | ||
} | ||
|
||
contents, err := fs.ReadFile(path) | ||
if err != nil { | ||
return []tsc.FoldingRange{}, err | ||
} | ||
|
||
doc := lsp.TextDocumentItem{ | ||
URI: params.TextDocument.URI, | ||
LanguageID: "tsc", | ||
Text: string(contents), | ||
} | ||
|
||
ranges := tsc.GetFoldingRanges(doc) | ||
return ranges, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tsc | ||
|
||
import ( | ||
"github.com/sourcegraph/go-lsp" | ||
"pkg.nimblebun.works/tsc-language-server/langserver/textdocument" | ||
"pkg.nimblebun.works/tsc-language-server/utils" | ||
) | ||
|
||
const ( | ||
// FRKComment resolves to the "comment" folding range kind. | ||
FRKComment = "comment" | ||
|
||
// FRKImports resolves to the "imports" folding range kind. | ||
FRKImports = "imports" | ||
|
||
// FRKRegion resolves to the "region" folding range kind. | ||
FRKRegion = "region" | ||
) | ||
|
||
// FoldingRange defines a LSP-compatible folding range. | ||
type FoldingRange struct { | ||
StartLine int `json:"startLine"` | ||
StartCharacter int `json:"startCharacter,omitempty"` | ||
EndLine int `json:"endLine"` | ||
EndCharacter int `json:"endCharacter"` | ||
|
||
Kind string `json:"kind,omitempty"` | ||
} | ||
|
||
func createFoldingRange(start int, end int, document textdocument.TextDocument) FoldingRange { | ||
startPos := document.PositionAt(start) | ||
endPos := document.PositionAt(end) | ||
|
||
return FoldingRange{ | ||
StartLine: startPos.Line, | ||
StartCharacter: startPos.Character, | ||
|
||
EndLine: endPos.Line, | ||
EndCharacter: endPos.Character, | ||
|
||
Kind: FRKRegion, | ||
} | ||
} | ||
|
||
// GetFoldingRanges will return all foldable ranges from a given document. | ||
func GetFoldingRanges(textDocumentItem lsp.TextDocumentItem) []FoldingRange { | ||
text := textDocumentItem.Text | ||
|
||
document := textdocument.From(textDocumentItem) | ||
ranges := make([]FoldingRange, 0) | ||
|
||
start := -1 | ||
end := -1 | ||
|
||
for idx, letter := range text { | ||
if letter == '#' && IsEvent(utils.Substring(text, idx, 5)) { | ||
if end-start > 4 { | ||
foldingRange := createFoldingRange(start, end, document) | ||
ranges = append(ranges, foldingRange) | ||
} | ||
|
||
start = idx | ||
} | ||
|
||
if letter != '\n' && letter != '\r' { | ||
end = idx | ||
} | ||
} | ||
|
||
if end-start > 4 { | ||
foldingRange := createFoldingRange(start, end, document) | ||
ranges = append(ranges, foldingRange) | ||
} | ||
|
||
return ranges | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package tsc_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sourcegraph/go-lsp" | ||
"pkg.nimblebun.works/tsc-language-server/tsc" | ||
) | ||
|
||
func TestGetFoldingRanges(t *testing.T) { | ||
dummyData := `#0098 | ||
<CNP0306:0117:0000<ANP0306:0032:0002<END | ||
|
||
#0100 | ||
<KEY<FLJ0839:0101 | ||
<SOU0011<ANP0100:0000:0002 | ||
<FAO0000<TRA0046:0090:0017:0009 | ||
|
||
#0101 | ||
<KEY<MSGIt won't open...<NOD<END | ||
|
||
#0200 | ||
<KEY | ||
<FLJ0832:0204 | ||
<FLJ0824:0203 | ||
<FLJ0823:0202 | ||
<FLJ0821:0201<MSG | ||
OPEN SHUTTER?<YNJ0000<CLR | ||
OPENING SHUTTER<NOD<CLO<MYD0000 | ||
<WAI0030<ANP0250:0010:0001<WAI0010<ANP0300:0001:0002 | ||
<WAI0022<ANP0251:0010:0001<ANP0300:0003:0002 | ||
<WAI0032<ANP0252:0010:0001 | ||
<WAI0032<ANP0253:0010:0001 | ||
<WAI0032<ANP0254:0010:0001<DNP0250 | ||
<WAI0032<DNP0251 | ||
<WAI0032<DNP0252 | ||
<ANP0253:0001:0000<WAI0032<DNP0300 | ||
<CNP0301:0117:0000 | ||
<ANP0301:0021:0002 | ||
<FL-0820<FL+0821<FL+0822<MSGABNORMALITY DETECTED IN | ||
SHUTTER NO. 4<NOD<END` | ||
|
||
dummyDocument := lsp.TextDocumentItem{ | ||
Text: dummyData, | ||
} | ||
|
||
t.Run("should return correct number of folding ranges", func(t *testing.T) { | ||
ranges := tsc.GetFoldingRanges(dummyDocument) | ||
actual := len(ranges) | ||
expected := 4 | ||
|
||
if expected != actual { | ||
t.Errorf("GetFoldingRanges(document) length, got %d, want %d", actual, expected) | ||
} | ||
}) | ||
|
||
t.Run("should return correct ranges", func(t *testing.T) { | ||
ranges := tsc.GetFoldingRanges(dummyDocument) | ||
expectedRanges := []tsc.FoldingRange{ | ||
{ | ||
StartLine: 0, | ||
StartCharacter: 0, | ||
EndLine: 1, | ||
EndCharacter: 39, | ||
}, | ||
{ | ||
StartLine: 3, | ||
StartCharacter: 0, | ||
EndLine: 6, | ||
EndCharacter: 30, | ||
}, | ||
{ | ||
StartLine: 8, | ||
StartCharacter: 0, | ||
EndLine: 9, | ||
EndCharacter: 31, | ||
}, | ||
{ | ||
StartLine: 11, | ||
StartCharacter: 0, | ||
EndLine: 30, | ||
EndCharacter: 20, | ||
}, | ||
} | ||
|
||
for idx := range ranges { | ||
actual := ranges[idx] | ||
expected := expectedRanges[idx] | ||
|
||
if actual.StartLine != expected.StartLine { | ||
t.Errorf("GetFoldingRanges(doc) @ %d -> StartLine, got %v, want %v", idx, actual.StartLine, expected.StartLine) | ||
} | ||
|
||
if actual.StartCharacter != expected.StartCharacter { | ||
t.Errorf("GetFoldingRanges(doc) @ %d -> StartCharacter, got %v, want %v", idx, actual.StartCharacter, expected.StartCharacter) | ||
} | ||
|
||
if actual.EndLine != expected.EndLine { | ||
t.Errorf("GetFoldingRanges(doc) @ %d -> EndLine, got %v, want %v", idx, actual.EndLine, expected.EndLine) | ||
} | ||
|
||
if actual.EndCharacter != expected.EndCharacter { | ||
t.Errorf("GetFoldingRanges(doc) @ %d -> EndCharacter, got %v, want %v", idx, actual.EndCharacter, expected.EndCharacter) | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.