Skip to content

Commit

Permalink
internal/highlight: always normalize filepath extensions before sendi…
Browse files Browse the repository at this point in the history
…ng to syntect_server

syntect_server expects lowercase filepath extensions only in https://github.com/sourcegraph/syntect_server#supported-file-extensions
technically, it is being correct here because the actual syntax definitions describe exactly
which file extensions they should match in a case sensitive manner. However, in practice
this is not ideal because there is not usually an overlap of case-sensitivity in file extensions
describing different syntaxes, and it is somewhat common for users to find they have e.g. `.CPP`
and `.cpp` files for whatever reason in their repos. Thus, we lowercase file extensions before
sending the request to syntect_server.

Fixes #13007

Fixes #11327
  • Loading branch information
slimsag committed Nov 23, 2020
1 parent dd02c3a commit 0f4142a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/highlight/highlight.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"html/template"
"net/http"
"path"
"strings"
"time"
"unicode/utf8"
Expand Down Expand Up @@ -165,6 +166,8 @@ func Code(ctx context.Context, p Params) (h template.HTML, aborted bool, err err
stabilizeTimeout = 30 * time.Second
}

p.Filepath = normalizeFilepath(p.Filepath)

resp, err := client.Highlight(ctx, &gosyntect.Query{
Code: code,
Filepath: p.Filepath,
Expand Down Expand Up @@ -510,3 +513,23 @@ func splitHighlightedLines(input template.HTML) ([]template.HTML, error) {

return lines, nil
}

// normalizeFilepath ensures that the filepath p has a lowercase extension, i.e. it applies the
// following transformations:
//
// a/b/c/FOO.TXT → a/b/c/FOO.txt
// FOO.Sh → FOO.sh
//
// The following are left unmodified, as they already have lowercase extensions:
//
// a/b/c/FOO.txt
// a/b/c/Makefile
// Makefile.am
// FOO.txt
//
// It expects the filepath uses forward slashes always.
func normalizeFilepath(p string) string {
ext := path.Ext(p)
ext = strings.ToLower(ext)
return p[:len(p)-len(ext)] + ext
}
47 changes: 47 additions & 0 deletions internal/highlight/highlight_test.go
Expand Up @@ -218,3 +218,50 @@ line3`
t.Fatalf("wrong highlighted lines: %s", diff)
}
}

func Test_normalizeFilepath(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "normalize_path",
input: "a/b/c/FOO.TXT",
want: "a/b/c/FOO.txt",
},
{
name: "normalize_partial_path",
input: "FOO.Sh",
want: "FOO.sh",
},
{
name: "unmodified_path",
input: "a/b/c/FOO.txt",
want: "a/b/c/FOO.txt",
},
{
name: "unmodified_path_no_extension",
input: "a/b/c/Makefile",
want: "a/b/c/Makefile",
},
{
name: "unmodified_partial_path_no_extension",
input: "Makefile",
want: "Makefile",
},
{
name: "unmodified_partial_path_extension",
input: "Makefile.am",
want: "Makefile.am",
},
}
for _, tst := range tests {
t.Run(tst.name, func(t *testing.T) {
got := normalizeFilepath(tst.input)
if diff := cmp.Diff(got, tst.want); diff != "" {
t.Fatalf("mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 0f4142a

Please sign in to comment.