aboutsummaryrefslogtreecommitdiff
path: root/gopls/internal/lsp/tests/normalizer.go
blob: 9c5d7b9c82f1fe4b2c743f20ba9881503a229174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tests

import (
	"path/filepath"
	"strconv"
	"strings"

	"golang.org/x/tools/go/packages/packagestest"
)

type Normalizer struct {
	path     string
	slashed  string
	escaped  string
	fragment string
}

func CollectNormalizers(exported *packagestest.Exported) []Normalizer {
	// build the path normalizing patterns
	var normalizers []Normalizer
	for _, m := range exported.Modules {
		for fragment := range m.Files {
			n := Normalizer{
				path:     exported.File(m.Name, fragment),
				fragment: fragment,
			}
			if n.slashed = filepath.ToSlash(n.path); n.slashed == n.path {
				n.slashed = ""
			}
			quoted := strconv.Quote(n.path)
			if n.escaped = quoted[1 : len(quoted)-1]; n.escaped == n.path {
				n.escaped = ""
			}
			normalizers = append(normalizers, n)
		}
	}
	return normalizers
}

// Normalize replaces all paths present in s with just the fragment portion
// this is used to make golden files not depend on the temporary paths of the files
func Normalize(s string, normalizers []Normalizer) string {
	type entry struct {
		path     string
		index    int
		fragment string
	}
	var match []entry
	// collect the initial state of all the matchers
	for _, n := range normalizers {
		index := strings.Index(s, n.path)
		if index >= 0 {
			match = append(match, entry{n.path, index, n.fragment})
		}
		if n.slashed != "" {
			index := strings.Index(s, n.slashed)
			if index >= 0 {
				match = append(match, entry{n.slashed, index, n.fragment})
			}
		}
		if n.escaped != "" {
			index := strings.Index(s, n.escaped)
			if index >= 0 {
				match = append(match, entry{n.escaped, index, n.fragment})
			}
		}
	}
	// result should be the same or shorter than the input
	var b strings.Builder
	last := 0
	for {
		// find the nearest path match to the start of the buffer
		next := -1
		nearest := len(s)
		for i, c := range match {
			if c.index >= 0 && nearest > c.index {
				nearest = c.index
				next = i
			}
		}
		// if there are no matches, we copy the rest of the string and are done
		if next < 0 {
			b.WriteString(s[last:])
			return b.String()
		}
		// we have a match
		n := &match[next]
		// copy up to the start of the match
		b.WriteString(s[last:n.index])
		// skip over the filename
		last = n.index + len(n.path)

		// Hack: In multi-module mode, we add a "testmodule/" prefix, so trim
		// it from the fragment.
		fragment := n.fragment
		if strings.HasPrefix(fragment, "testmodule") {
			split := strings.Split(filepath.ToSlash(fragment), "/")
			fragment = filepath.FromSlash(strings.Join(split[1:], "/"))
		}

		// add in the fragment instead
		b.WriteString(fragment)
		// see what the next match for this path is
		n.index = strings.Index(s[last:], n.path)
		if n.index >= 0 {
			n.index += last
		}
	}
}