|
| 1 | +package lsp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +const defaultInitializeID = 1 |
| 14 | + |
| 15 | +type ServerHandshakeOptions struct { |
| 16 | + InitializeID int |
| 17 | + ProcessID int |
| 18 | + RootURI string |
| 19 | + RootPath string |
| 20 | + ClientName string |
| 21 | + ClientVersion string |
| 22 | + Trace string |
| 23 | + Documents []OpenDocument |
| 24 | +} |
| 25 | + |
| 26 | +type OpenDocument struct { |
| 27 | + URI string |
| 28 | + FilePath string |
| 29 | + LanguageID string |
| 30 | + Version int |
| 31 | + Text string |
| 32 | +} |
| 33 | + |
| 34 | +type textDocumentItem struct { |
| 35 | + URI string `json:"uri"` |
| 36 | + LanguageID string `json:"languageId"` |
| 37 | + Version int `json:"version"` |
| 38 | + Text string `json:"text"` |
| 39 | +} |
| 40 | + |
| 41 | +type jsonRPCMessage struct { |
| 42 | + JSONRPC string `json:"jsonrpc"` |
| 43 | + ID int `json:"id,omitempty"` |
| 44 | + Method string `json:"method"` |
| 45 | + Params any `json:"params,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +func (p *ServerProcess) InitializeAndOpen(ctx context.Context, opts ServerHandshakeOptions) error { |
| 49 | + if p == nil || p.stdin == nil { |
| 50 | + return os.ErrInvalid |
| 51 | + } |
| 52 | + return WriteInitializeAndOpen(ctx, p.stdin, opts) |
| 53 | +} |
| 54 | + |
| 55 | +func WriteInitializeAndOpen(ctx context.Context, w io.Writer, opts ServerHandshakeOptions) error { |
| 56 | + if ctx == nil { |
| 57 | + ctx = context.Background() |
| 58 | + } |
| 59 | + if w == nil { |
| 60 | + return os.ErrInvalid |
| 61 | + } |
| 62 | + if err := WriteInitializeRequest(ctx, w, opts); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + if err := WriteInitializedNotification(ctx, w); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + for _, doc := range opts.Documents { |
| 69 | + if err := WriteDidOpenNotification(ctx, w, doc); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func WriteInitializeRequest(ctx context.Context, w io.Writer, opts ServerHandshakeOptions) error { |
| 77 | + if err := contextErr(ctx); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + if w == nil { |
| 81 | + return os.ErrInvalid |
| 82 | + } |
| 83 | + id := opts.InitializeID |
| 84 | + if id == 0 { |
| 85 | + id = defaultInitializeID |
| 86 | + } |
| 87 | + processID := opts.ProcessID |
| 88 | + if processID == 0 { |
| 89 | + processID = os.Getpid() |
| 90 | + } |
| 91 | + clientName := strings.TrimSpace(opts.ClientName) |
| 92 | + if clientName == "" { |
| 93 | + clientName = "ccgo" |
| 94 | + } |
| 95 | + params := map[string]any{ |
| 96 | + "processId": processID, |
| 97 | + "clientInfo": map[string]any{ |
| 98 | + "name": clientName, |
| 99 | + "version": strings.TrimSpace(opts.ClientVersion), |
| 100 | + }, |
| 101 | + "capabilities": defaultClientCapabilities(), |
| 102 | + } |
| 103 | + if rootURI := strings.TrimSpace(opts.RootURI); rootURI != "" { |
| 104 | + params["rootUri"] = rootURI |
| 105 | + } |
| 106 | + if rootPath := strings.TrimSpace(opts.RootPath); rootPath != "" { |
| 107 | + params["rootPath"] = rootPath |
| 108 | + } |
| 109 | + if trace := strings.TrimSpace(opts.Trace); trace != "" { |
| 110 | + params["trace"] = trace |
| 111 | + } |
| 112 | + return writeFramedJSON(w, jsonRPCMessage{ |
| 113 | + JSONRPC: "2.0", |
| 114 | + ID: id, |
| 115 | + Method: "initialize", |
| 116 | + Params: params, |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func WriteInitializedNotification(ctx context.Context, w io.Writer) error { |
| 121 | + if err := contextErr(ctx); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + if w == nil { |
| 125 | + return os.ErrInvalid |
| 126 | + } |
| 127 | + return writeFramedJSON(w, jsonRPCMessage{ |
| 128 | + JSONRPC: "2.0", |
| 129 | + Method: "initialized", |
| 130 | + Params: map[string]any{}, |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +func WriteDidOpenNotification(ctx context.Context, w io.Writer, doc OpenDocument) error { |
| 135 | + if err := contextErr(ctx); err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + if w == nil { |
| 139 | + return os.ErrInvalid |
| 140 | + } |
| 141 | + item, err := normalizeOpenDocument(doc) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + return writeFramedJSON(w, jsonRPCMessage{ |
| 146 | + JSONRPC: "2.0", |
| 147 | + Method: "textDocument/didOpen", |
| 148 | + Params: map[string]any{ |
| 149 | + "textDocument": item, |
| 150 | + }, |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +func FileURIFromPath(path string) string { |
| 155 | + path = strings.TrimSpace(path) |
| 156 | + if path == "" { |
| 157 | + return "" |
| 158 | + } |
| 159 | + cleaned := filepath.Clean(path) |
| 160 | + if !filepath.IsAbs(cleaned) { |
| 161 | + if abs, err := filepath.Abs(cleaned); err == nil { |
| 162 | + cleaned = abs |
| 163 | + } |
| 164 | + } |
| 165 | + u := url.URL{Scheme: "file", Path: filepath.ToSlash(cleaned)} |
| 166 | + return u.String() |
| 167 | +} |
| 168 | + |
| 169 | +func defaultClientCapabilities() map[string]any { |
| 170 | + return map[string]any{ |
| 171 | + "textDocument": map[string]any{ |
| 172 | + "publishDiagnostics": map[string]any{ |
| 173 | + "relatedInformation": true, |
| 174 | + }, |
| 175 | + }, |
| 176 | + "workspace": map[string]any{ |
| 177 | + "workspaceFolders": true, |
| 178 | + }, |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func normalizeOpenDocument(doc OpenDocument) (textDocumentItem, error) { |
| 183 | + uri := strings.TrimSpace(doc.URI) |
| 184 | + if uri == "" { |
| 185 | + uri = FileURIFromPath(doc.FilePath) |
| 186 | + } |
| 187 | + if uri == "" { |
| 188 | + return textDocumentItem{}, os.ErrInvalid |
| 189 | + } |
| 190 | + languageID := strings.TrimSpace(doc.LanguageID) |
| 191 | + if languageID == "" { |
| 192 | + languageID = languageIDFromPath(doc.FilePath) |
| 193 | + } |
| 194 | + if languageID == "" { |
| 195 | + languageID = "plaintext" |
| 196 | + } |
| 197 | + version := doc.Version |
| 198 | + if version == 0 { |
| 199 | + version = 1 |
| 200 | + } |
| 201 | + return textDocumentItem{ |
| 202 | + URI: uri, |
| 203 | + LanguageID: languageID, |
| 204 | + Version: version, |
| 205 | + Text: doc.Text, |
| 206 | + }, nil |
| 207 | +} |
| 208 | + |
| 209 | +func languageIDFromPath(path string) string { |
| 210 | + switch strings.ToLower(filepath.Ext(strings.TrimSpace(path))) { |
| 211 | + case ".c", ".h": |
| 212 | + return "c" |
| 213 | + case ".cc", ".cpp", ".cxx", ".hh", ".hpp", ".hxx": |
| 214 | + return "cpp" |
| 215 | + case ".go": |
| 216 | + return "go" |
| 217 | + case ".js", ".jsx", ".mjs", ".cjs": |
| 218 | + return "javascript" |
| 219 | + case ".json": |
| 220 | + return "json" |
| 221 | + case ".lua": |
| 222 | + return "lua" |
| 223 | + case ".md", ".markdown": |
| 224 | + return "markdown" |
| 225 | + case ".py": |
| 226 | + return "python" |
| 227 | + case ".rs": |
| 228 | + return "rust" |
| 229 | + case ".ts", ".tsx": |
| 230 | + return "typescript" |
| 231 | + default: |
| 232 | + return "" |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +func writeFramedJSON(w io.Writer, value any) error { |
| 237 | + data, err := json.Marshal(value) |
| 238 | + if err != nil { |
| 239 | + return err |
| 240 | + } |
| 241 | + return WriteFramedMessage(w, data) |
| 242 | +} |
| 243 | + |
| 244 | +func contextErr(ctx context.Context) error { |
| 245 | + if ctx == nil { |
| 246 | + return nil |
| 247 | + } |
| 248 | + return ctx.Err() |
| 249 | +} |
0 commit comments