Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lsp): ignore diagnostic check within code blocks #399

Merged
merged 10 commits into from
Apr 6, 2024
Prev Previous commit
Next Next commit
wip: refactor if statement into function
  • Loading branch information
tjex committed Apr 5, 2024
commit b68df7f87ea9c759bd8175f239755e0535c22dad
79 changes: 44 additions & 35 deletions internal/adapter/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ var fencedStartRegex = regexp.MustCompile(`^(` + "```" + `|~~~).*`)
var fencedEndRegex = regexp.MustCompile(`^(` + "```" + `|~~~)\s*`)
var indentedRegex = regexp.MustCompile(`^(\s{4}|\t).+`)

var insideInline = false
var insideFenced = false
var insideIndented = false
var currentCodeBlockStart = -1

// LinkFromRoot returns a Link to this document from the root of the given
// notebook.
func (d *document) LinkFromRoot(nb *core.Notebook) (*documentLink, error) {
Expand Down Expand Up @@ -207,49 +212,53 @@ func linkWithinInlineCode(strBuffer string, linkStart, linkEnd int, insideInline
}
}

func ignoreLinesInCodeBlocks(lines []string, lineIndex int, line string) bool {
// Checks to ignore lines within code fences and indented code blocks
if insideFenced {
if fencedEndRegex.FindStringIndex(line) != nil &&
lines[currentCodeBlockStart][:3] == line[:3] {
// Fenced code block ends with this line
insideFenced = false
currentCodeBlockStart = -1
}
return true
} else if insideIndented {
if indentedRegex.FindStringIndex(line) == nil && len(line) > 0 {
// Indeted code block ends with this line
insideIndented = false
currentCodeBlockStart = -1
} else {
return true
}
} else {
// Check whether the current line is the start of a code fence or
// indented code block
if fencedStartRegex.FindStringIndex(line) != nil {
insideFenced = true
currentCodeBlockStart = lineIndex
return true
} else if indentedRegex.FindStringIndex(line) != nil &&
(lineIndex > 0 && len(lines[lineIndex-1]) == 0 || lineIndex == 0) {
insideIndented = true
currentCodeBlockStart = lineIndex
return true
}
}
return false

}

// DocumentLinks returns all the internal and external links found in the
// document.
func (d *document) DocumentLinks() ([]documentLink, error) {
links := []documentLink{}

insideInline := false
insideFenced := false
insideIndented := false
currentCodeBlockStart := -1
lines := d.GetLines()
for lineIndex, line := range lines {

// Checks to ignore lines within code fences and indented code blocks
if insideFenced {
if fencedEndRegex.FindStringIndex(line) != nil &&
lines[currentCodeBlockStart][:3] == line[:3] {
// Fenced code block ends with this line
insideFenced = false
currentCodeBlockStart = -1
}
continue
} else if insideIndented {
if indentedRegex.FindStringIndex(line) == nil && len(line) > 0 {
// Indeted code block ends with this line
insideIndented = false
currentCodeBlockStart = -1
} else {
continue
}
} else {
// Check whether the current line is the start of a code fence or
// indented code block
if fencedStartRegex.FindStringIndex(line) != nil {
insideFenced = true
currentCodeBlockStart = lineIndex
continue
} else if indentedRegex.FindStringIndex(line) != nil &&
(lineIndex > 0 && len(lines[lineIndex-1]) == 0 || lineIndex == 0) {
insideIndented = true
currentCodeBlockStart = lineIndex
continue
}
}
if ignoreLinesInCodeBlocks(lines, lineIndex, line) {
continue
}

appendLink := func(href string, start, end int, hasTitle bool, isWikiLink bool) {
if href == "" {
Expand Down