Skip to content

Commit

Permalink
map to go source and back for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
jackielii committed Oct 21, 2024
1 parent 90c3f99 commit a9ca1f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
10 changes: 5 additions & 5 deletions cmd/templ/lspcmd/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,18 +659,18 @@ func TestDocumentSymbol(t *testing.T) {
cursor: ` ^`,
expect: []any{
protocol.SymbolInformation{
Name: "Page(count int)",
Name: "Page",
Kind: protocol.SymbolKindFunction,
Location: protocol.Location{
URI: uri.URI("file://" + appDir + "/templates.templ"),
Range: protocol.Range{
Start: protocol.Position{
Line: 4,
Character: 6,
Line: 11,
Character: 0,
},
End: protocol.Position{
Line: 4,
Character: 21,
Line: 50,
Character: 1,
},
},
},
Expand Down
59 changes: 25 additions & 34 deletions cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,56 +826,47 @@ func (p *Server) DocumentLinkResolve(ctx context.Context, params *lsp.DocumentLi
func (p *Server) DocumentSymbol(ctx context.Context, params *lsp.DocumentSymbolParams) (result []interface{} /* []SymbolInformation | []DocumentSymbol */, err error) {
p.Log.Info("client -> server: DocumentSymbol")
defer p.Log.Info("client -> server: DocumentSymbol end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.DocumentSymbol(ctx, params)
}
templURI := params.TextDocument.URI
params.TextDocument.URI = goURI
symbols, err := p.Target.DocumentSymbol(ctx, params)
if err != nil {
return nil, err
}
requireDocumentSymbols := false

// recursively convert the ranges of the symbols and their children
var convertRange func(s *lsp.DocumentSymbol)
convertRange = func(s *lsp.DocumentSymbol) {
s.Range = p.convertGoRangeToTemplRange(templURI, s.Range)
s.SelectionRange = p.convertGoRangeToTemplRange(templURI, s.SelectionRange)
for i := 0; i < len(s.Children); i++ {
convertRange(&s.Children[i])
}
}

for _, s := range symbols {
if m, ok := s.(map[string]interface{}); ok {
s, err = mapToSymbol(m)
if err != nil {
return nil, err
}
}
switch s.(type) {
switch s := s.(type) {
case lsp.DocumentSymbol:
requireDocumentSymbols = true
convertRange(&s)
result = append(result, s)
case lsp.SymbolInformation:
// p.Log.Info("symbole range before", zap.Any("range", s.Location.Range), zap.String("uri", string(s.Location.URI)))
s.Location.URI = templURI
s.Location.Range = p.convertGoRangeToTemplRange(templURI, s.Location.Range)
// p.Log.Info("symbole range after", zap.Any("range", s.Location.Range), zap.String("uri", string(s.Location.URI)))
result = append(result, s)
}
result = append(result, s)
}

// TODO: it looks like we only have SymbolInformation in the result. We should handle DocumentSymbol as well.
_ = requireDocumentSymbols

templSymbols := []interface{}{}
doc, ok := p.TemplSource.Get(string(params.TextDocument.URI))
if ok {
template, err := parser.ParseString(doc.String())
if err == nil {
for _, s := range template.Nodes {
switch s := s.(type) {
case parser.TemplateFileGoExpression:
case parser.HTMLTemplate:
templSymbols = append(templSymbols, lsp.SymbolInformation{
Name: s.Expression.Value,
Kind: lsp.SymbolKindFunction,
Location: lsp.Location{
URI: params.TextDocument.URI,
Range: parserRangeToLspRange(s.Expression.Range),
},
})
case parser.CSSTemplate:
case parser.ScriptTemplate:
}
}
}
}
// Put the templ symbols first.
if len(templSymbols) > 0 {
result = append(templSymbols, result...)
}
return result, err
}

Expand Down

0 comments on commit a9ca1f4

Please sign in to comment.