Skip to content

Commit

Permalink
fix: update ftl gen lsp to ignore non go docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gak committed Aug 27, 2024
1 parent cf92f3b commit 1e14c99
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
65 changes: 65 additions & 0 deletions cmd/ftl-gen-lsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"

"github.com/alecthomas/kong"
"github.com/tliron/kutil/terminal"
)

type CLI struct {
Expand Down Expand Up @@ -63,6 +65,8 @@ func scrapeDocs(hovers []hover) (map[string]string, error) {
return nil, fmt.Errorf("failed to read %s: %w", path, err)
}

doc.Content = stripNonGoDocs(doc.Content)

var content string
if len(hover.Select) > 0 {
for _, sel := range hover.Select {
Expand All @@ -78,6 +82,19 @@ func scrapeDocs(hovers []hover) (map[string]string, error) {
}

items[hover.Match] = content

// get term width or 80 if not available
width := 80
if w, _, err := terminal.GetSize(); err == nil {
width = w
}
line := strings.Repeat("-", width)
fmt.Print("\x1b[1;34m")
fmt.Println(line)
fmt.Println(hover.Match)
fmt.Println(line)
fmt.Print("\x1b[0m")
fmt.Println(content)
}
return items, nil
}
Expand Down Expand Up @@ -133,6 +150,54 @@ func getMarkdownWithTitle(file *os.File) (*Doc, error) {
return &Doc{Title: title, Content: string(parts[2])}, nil
}

var HTMLCommentRegex = regexp.MustCompile(`<!--\w*(.*?)\w*-->`)

// stripNonGoDocs removes non-Go code blocks from the markdown content.
func stripNonGoDocs(content string) string {
lines := strings.Split(content, "\n")

var currentLang string
var collected []string
for _, line := range lines {
if strings.Contains(line, "code_selector()") {
currentLang = ""
continue
}
if line == "{% end %}" {
currentLang = ""
continue
}

// Grab the language from the HTML comment text
values := HTMLCommentRegex.FindStringSubmatch(line)
if len(values) > 1 {
lang := strings.TrimSpace(values[1])
// end starts "common" text block
if lang == "end" {
currentLang = ""
} else {
currentLang = lang
}
continue
}

// Similar, look at the code block
if strings.HasPrefix(line, "```") {
newLang := strings.TrimPrefix(line, "```")
if newLang != "" {
currentLang = newLang
}
}

// If we're in a Go block or unspecified, collect the line.
if currentLang == "go" || currentLang == "" {
collected = append(collected, line)
}
}

return strings.Join(collected, "\n")
}

func selector(content, selector string) (string, error) {
// Split the content into lines.
lines := strings.Split(content, "\n")
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/hover.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"match": "//ftl:retry",
"source": "reference/retries.md"
}
]
]
6 changes: 3 additions & 3 deletions internal/lsp/hoveritems.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e14c99

Please sign in to comment.