| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package fake |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"path" |
| 12 |
"path/filepath" |
| 13 |
"sync/atomic" |
| 14 |
|
| 15 |
"golang.org/x/tools/gopls/internal/protocol" |
| 16 |
"golang.org/x/tools/gopls/internal/test/integration/fake/glob" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type ClientHooks struct { |
| 24 |
OnLogMessage func(context.Context, *protocol.LogMessageParams) error |
| 25 |
OnDiagnostics func(context.Context, *protocol.PublishDiagnosticsParams) error |
| 26 |
OnWorkDoneProgressCreate func(context.Context, *protocol.WorkDoneProgressCreateParams) error |
| 27 |
OnProgress func(context.Context, *protocol.ProgressParams) error |
| 28 |
OnShowDocument func(context.Context, *protocol.ShowDocumentParams) error |
| 29 |
OnShowMessage func(context.Context, *protocol.ShowMessageParams) error |
| 30 |
OnShowMessageRequest func(context.Context, *protocol.ShowMessageRequestParams) error |
| 31 |
OnRegisterCapability func(context.Context, *protocol.RegistrationParams) error |
| 32 |
OnUnregisterCapability func(context.Context, *protocol.UnregistrationParams) error |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type Client struct { |
| 39 |
editor *Editor |
| 40 |
hooks ClientHooks |
| 41 |
onApplyEdit atomic.Pointer[ApplyEditHandler] |
| 42 |
} |
| 43 |
|
| 44 |
type ApplyEditHandler = func(context.Context, *protocol.WorkspaceEdit) error |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
func (c *Client) SetApplyEditHandler(h ApplyEditHandler) func() { |
| 51 |
if h == nil { |
| 52 |
panic("h is nil") |
| 53 |
} |
| 54 |
prev := c.onApplyEdit.Swap(&h) |
| 55 |
return func() { |
| 56 |
if c.onApplyEdit.Swap(prev) != &h { |
| 57 |
panic("improper nesting of SetApplyEditHandler, restore") |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func (c *Client) CodeLensRefresh(context.Context) error { return nil } |
| 63 |
|
| 64 |
func (c *Client) InlayHintRefresh(context.Context) error { return nil } |
| 65 |
|
| 66 |
func (c *Client) DiagnosticRefresh(context.Context) error { return nil } |
| 67 |
|
| 68 |
func (c *Client) FoldingRangeRefresh(context.Context) error { return nil } |
| 69 |
|
| 70 |
func (c *Client) InlineValueRefresh(context.Context) error { return nil } |
| 71 |
|
| 72 |
func (c *Client) SemanticTokensRefresh(context.Context) error { return nil } |
| 73 |
|
| 74 |
func (c *Client) LogTrace(context.Context, *protocol.LogTraceParams) error { return nil } |
| 75 |
|
| 76 |
func (c *Client) TextDocumentContentRefresh(context.Context, *protocol.TextDocumentContentRefreshParams) error { |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
func (c *Client) ShowMessage(ctx context.Context, params *protocol.ShowMessageParams) error { |
| 81 |
if c.hooks.OnShowMessage != nil { |
| 82 |
return c.hooks.OnShowMessage(ctx, params) |
| 83 |
} |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
func (c *Client) ShowMessageRequest(ctx context.Context, params *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error) { |
| 88 |
if c.hooks.OnShowMessageRequest != nil { |
| 89 |
if err := c.hooks.OnShowMessageRequest(ctx, params); err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
} |
| 93 |
if c.editor.config.MessageResponder != nil { |
| 94 |
return c.editor.config.MessageResponder(params) |
| 95 |
} |
| 96 |
return nil, nil |
| 97 |
} |
| 98 |
|
| 99 |
func (c *Client) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error { |
| 100 |
if c.hooks.OnLogMessage != nil { |
| 101 |
return c.hooks.OnLogMessage(ctx, params) |
| 102 |
} |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
func (c *Client) Event(ctx context.Context, event *any) error { |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func (c *Client) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) error { |
| 111 |
if c.hooks.OnDiagnostics != nil { |
| 112 |
return c.hooks.OnDiagnostics(ctx, params) |
| 113 |
} |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, error) { |
| 118 |
return []protocol.WorkspaceFolder{}, nil |
| 119 |
} |
| 120 |
|
| 121 |
func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]any, error) { |
| 122 |
results := make([]any, len(p.Items)) |
| 123 |
for i, item := range p.Items { |
| 124 |
if item.ScopeURI != nil && *item.ScopeURI == "" { |
| 125 |
return nil, fmt.Errorf(`malformed ScopeURI ""`) |
| 126 |
} |
| 127 |
if item.Section == "gopls" { |
| 128 |
config := c.editor.Config() |
| 129 |
results[i] = makeSettings(c.editor.sandbox, config, item.ScopeURI) |
| 130 |
} |
| 131 |
} |
| 132 |
return results, nil |
| 133 |
} |
| 134 |
|
| 135 |
func (c *Client) RegisterCapability(ctx context.Context, params *protocol.RegistrationParams) error { |
| 136 |
if c.hooks.OnRegisterCapability != nil { |
| 137 |
if err := c.hooks.OnRegisterCapability(ctx, params); err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
for _, registration := range params.Registrations { |
| 147 |
if registration.Method == "workspace/didChangeWatchedFiles" { |
| 148 |
|
| 149 |
|
| 150 |
raw, err := json.Marshal(registration.RegisterOptions) |
| 151 |
if err != nil { |
| 152 |
return fmt.Errorf("marshaling registration options: %v", err) |
| 153 |
} |
| 154 |
var opts protocol.DidChangeWatchedFilesRegistrationOptions |
| 155 |
if err := json.Unmarshal(raw, &opts); err != nil { |
| 156 |
return fmt.Errorf("unmarshaling registration options: %v", err) |
| 157 |
} |
| 158 |
var globs []*glob.Glob |
| 159 |
for _, watcher := range opts.Watchers { |
| 160 |
var globPattern string |
| 161 |
switch pattern := watcher.GlobPattern.Value.(type) { |
| 162 |
case protocol.Pattern: |
| 163 |
globPattern = pattern |
| 164 |
case protocol.RelativePattern: |
| 165 |
globPattern = path.Join(filepath.ToSlash(pattern.BaseURI.Path()), pattern.Pattern) |
| 166 |
} |
| 167 |
|
| 168 |
g, err := glob.Parse(globPattern) |
| 169 |
if err != nil { |
| 170 |
return fmt.Errorf("error parsing glob pattern %q: %v", watcher.GlobPattern, err) |
| 171 |
} |
| 172 |
globs = append(globs, g) |
| 173 |
} |
| 174 |
c.editor.mu.Lock() |
| 175 |
c.editor.watchPatterns = globs |
| 176 |
c.editor.mu.Unlock() |
| 177 |
} |
| 178 |
} |
| 179 |
return nil |
| 180 |
} |
| 181 |
|
| 182 |
func (c *Client) UnregisterCapability(ctx context.Context, params *protocol.UnregistrationParams) error { |
| 183 |
if c.hooks.OnUnregisterCapability != nil { |
| 184 |
return c.hooks.OnUnregisterCapability(ctx, params) |
| 185 |
} |
| 186 |
return nil |
| 187 |
} |
| 188 |
|
| 189 |
func (c *Client) Progress(ctx context.Context, params *protocol.ProgressParams) error { |
| 190 |
if c.hooks.OnProgress != nil { |
| 191 |
return c.hooks.OnProgress(ctx, params) |
| 192 |
} |
| 193 |
return nil |
| 194 |
} |
| 195 |
|
| 196 |
func (c *Client) WorkDoneProgressCreate(ctx context.Context, params *protocol.WorkDoneProgressCreateParams) error { |
| 197 |
if c.hooks.OnWorkDoneProgressCreate != nil { |
| 198 |
return c.hooks.OnWorkDoneProgressCreate(ctx, params) |
| 199 |
} |
| 200 |
return nil |
| 201 |
} |
| 202 |
|
| 203 |
func (c *Client) ShowDocument(ctx context.Context, params *protocol.ShowDocumentParams) (*protocol.ShowDocumentResult, error) { |
| 204 |
if c.hooks.OnShowDocument != nil { |
| 205 |
if err := c.hooks.OnShowDocument(ctx, params); err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
return &protocol.ShowDocumentResult{Success: true}, nil |
| 209 |
} |
| 210 |
return nil, nil |
| 211 |
} |
| 212 |
|
| 213 |
func (c *Client) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResult, error) { |
| 214 |
if len(params.Edit.Changes) > 0 { |
| 215 |
return &protocol.ApplyWorkspaceEditResult{FailureReason: "Edit.Changes is unsupported"}, nil |
| 216 |
} |
| 217 |
onApplyEdit := c.editor.applyWorkspaceEdit |
| 218 |
if ptr := c.onApplyEdit.Load(); ptr != nil { |
| 219 |
onApplyEdit = *ptr |
| 220 |
} |
| 221 |
if err := onApplyEdit(ctx, ¶ms.Edit); err != nil { |
| 222 |
return nil, err |
| 223 |
} |
| 224 |
return &protocol.ApplyWorkspaceEditResult{Applied: true}, nil |
| 225 |
} |