| 1 | import { getUriFileExtension } from "../../util/uri"; |
| 2 | import { BracketMatchingService } from "../filtering/BracketMatchingService"; |
| 3 | import { |
| 4 | CharacterFilter, |
| 5 | LineFilter, |
| 6 | } from "../filtering/streamTransforms/lineStream"; |
| 7 | |
| 8 | export interface AutocompleteLanguageInfo { |
| 9 | /** |
| 10 | * The display name of the language |
| 11 | */ |
| 12 | name: string; |
| 13 | /** |
| 14 | * Keywords that show up at the top-level of files. We often use these as stop words or soft breaks |
| 15 | */ |
| 16 | topLevelKeywords: string[]; |
| 17 | /** |
| 18 | * The character that prefixes a single line comment |
| 19 | * We use this to format snippets from other files as large comment blocks |
| 20 | */ |
| 21 | singleLineComment?: string; |
| 22 | /** |
| 23 | * Semi-colon or other end of line indicator that may be used as a stop char |
| 24 | * or other parsing |
| 25 | */ |
| 26 | endOfLine: string[]; |
| 27 | /** |
| 28 | * Strings that indicate we should filter out a line from completion |
| 29 | * Primarily currently used for .ipynb files |
| 30 | */ |
| 31 | lineFilters?: LineFilter[]; |
| 32 | /** |
| 33 | * Similar to line filters, but characters. I think. |
| 34 | */ |
| 35 | charFilters?: CharacterFilter[]; |
| 36 | /** |
| 37 | * Function that allows cusotmization of whether to use a multi-line completion on a per-language and completion basis |
| 38 | */ |
| 39 | useMultiline?: (args: { prefix: string; suffix: string }) => boolean; |
| 40 | } |
| 41 | |
| 42 | // TypeScript |
| 43 | export const Typescript = { |
| 44 | name: "TypeScript", |
| 45 | topLevelKeywords: ["function", "class", "module", "export", "import"], |
| 46 | singleLineComment: "//", |
| 47 | endOfLine: [";"], |
| 48 | }; |
| 49 | |
| 50 | // JavaScript |
| 51 | export const JavaScript = { |
| 52 | name: "JavaScript", |
| 53 | topLevelKeywords: ["function", "class", "module", "export", "import"], |
| 54 | singleLineComment: "//", |
| 55 | endOfLine: [";"], |
| 56 | }; |
| 57 | |
| 58 | // Python |
| 59 | export const Python = { |
| 60 | name: "Python", |
| 61 | // """"#" is for .ipynb files, where we add '"""' surrounding markdown blocks. |
| 62 | // This stops the model from trying to complete the start of a new markdown block |
| 63 | topLevelKeywords: ["def", "class", '"""#'], |
| 64 | singleLineComment: "#", |
| 65 | endOfLine: [], |
| 66 | }; |
| 67 | |
| 68 | // Java |
| 69 | export const Java = { |
| 70 | name: "Java", |
| 71 | topLevelKeywords: ["class", "function"], |
| 72 | singleLineComment: "//", |
| 73 | endOfLine: [";"], |
| 74 | }; |
| 75 | |
| 76 | // C++ |
| 77 | export const Cpp = { |
| 78 | name: "C++", |
| 79 | topLevelKeywords: ["class", "namespace", "template"], |
| 80 | singleLineComment: "//", |
| 81 | endOfLine: [";"], |
| 82 | }; |
| 83 | |
| 84 | // C# |
| 85 | export const CSharp = { |
| 86 | name: "C#", |
| 87 | topLevelKeywords: ["class", "namespace", "void"], |
| 88 | singleLineComment: "//", |
| 89 | endOfLine: [";"], |
| 90 | }; |
| 91 | |
| 92 | // C |
| 93 | export const C = { |
| 94 | name: "C", |
| 95 | topLevelKeywords: ["if", "else", "while", "for", "switch", "case"], |
| 96 | singleLineComment: "//", |
| 97 | endOfLine: [";"], |
| 98 | }; |
| 99 | |
| 100 | // Scala |
| 101 | export const Scala = { |
| 102 | name: "Scala", |
| 103 | topLevelKeywords: ["def", "val", "var", "class", "object", "trait"], |
| 104 | singleLineComment: "//", |
| 105 | endOfLine: [";"], |
| 106 | }; |
| 107 | |
| 108 | // Go |
| 109 | export const Go = { |
| 110 | name: "Go", |
| 111 | topLevelKeywords: ["func", "package", "import", "type"], |
| 112 | singleLineComment: "//", |
| 113 | endOfLine: [], |
| 114 | }; |
| 115 | |
| 116 | // Rust |
| 117 | export const Rust = { |
| 118 | name: "Rust", |
| 119 | topLevelKeywords: ["fn", "mod", "pub", "struct", "enum", "trait"], |
| 120 | singleLineComment: "//", |
| 121 | endOfLine: [";"], |
| 122 | }; |
| 123 | |
| 124 | // Haskell |
| 125 | export const Haskell = { |
| 126 | name: "Haskell", |
| 127 | topLevelKeywords: [ |
| 128 | "data", |
| 129 | "type", |
| 130 | "newtype", |
| 131 | "class", |
| 132 | "instance", |
| 133 | "let", |
| 134 | "in", |
| 135 | "where", |
| 136 | ], |
| 137 | singleLineComment: "--", |
| 138 | endOfLine: [], |
| 139 | }; |
| 140 | |
| 141 | // PHP |
| 142 | export const PHP = { |
| 143 | name: "PHP", |
| 144 | topLevelKeywords: ["function", "class", "namespace", "use"], |
| 145 | singleLineComment: "//", |
| 146 | endOfLine: [";"], |
| 147 | }; |
| 148 | |
| 149 | // Ruby on Rails |
| 150 | export const RubyOnRails = { |
| 151 | name: "Ruby on Rails", |
| 152 | topLevelKeywords: ["def", "class", "module"], |
| 153 | singleLineComment: "#", |
| 154 | endOfLine: [], |
| 155 | }; |
| 156 | |
| 157 | // Swift |
| 158 | export const Swift = { |
| 159 | name: "Swift", |
| 160 | topLevelKeywords: ["func", "class", "struct", "import"], |
| 161 | singleLineComment: "//", |
| 162 | endOfLine: [";"], |
| 163 | }; |
| 164 | |
| 165 | // Kotlin |
| 166 | export const Kotlin = { |
| 167 | name: "Kotlin", |
| 168 | topLevelKeywords: ["fun", "class", "package", "import"], |
| 169 | singleLineComment: "//", |
| 170 | endOfLine: [";"], |
| 171 | }; |
| 172 | |
| 173 | // Ruby |
| 174 | export const Ruby = { |
| 175 | name: "Ruby", |
| 176 | topLevelKeywords: ["class", "module", "def"], |
| 177 | singleLineComment: "#", |
| 178 | endOfLine: [], |
| 179 | }; |
| 180 | |
| 181 | // Clojure |
| 182 | export const Clojure = { |
| 183 | name: "Clojure", |
| 184 | topLevelKeywords: ["def", "fn", "let", "do", "if", "defn", "ns", "defmacro"], |
| 185 | singleLineComment: ";", |
| 186 | endOfLine: [], |
| 187 | }; |
| 188 | |
| 189 | // Julia |
| 190 | export const Julia = { |
| 191 | name: "Julia", |
| 192 | topLevelKeywords: [ |
| 193 | "function", |
| 194 | "macro", |
| 195 | "if", |
| 196 | "else", |
| 197 | "elseif", |
| 198 | "while", |
| 199 | "for", |
| 200 | "begin", |
| 201 | "end", |
| 202 | "module", |
| 203 | ], |
| 204 | singleLineComment: "#", |
| 205 | endOfLine: [";"], |
| 206 | }; |
| 207 | |
| 208 | // F# |
| 209 | export const FSharp = { |
| 210 | name: "F#", |
| 211 | topLevelKeywords: [ |
| 212 | "let", |
| 213 | "type", |
| 214 | "module", |
| 215 | "namespace", |
| 216 | "open", |
| 217 | "if", |
| 218 | "then", |
| 219 | "else", |
| 220 | "match", |
| 221 | "with", |
| 222 | ], |
| 223 | singleLineComment: "//", |
| 224 | endOfLine: [], |
| 225 | }; |
| 226 | |
| 227 | // R |
| 228 | export const R = { |
| 229 | name: "R", |
| 230 | topLevelKeywords: [ |
| 231 | "function", |
| 232 | "if", |
| 233 | "else", |
| 234 | "for", |
| 235 | "while", |
| 236 | "repeat", |
| 237 | "library", |
| 238 | "require", |
| 239 | ], |
| 240 | singleLineComment: "#", |
| 241 | endOfLine: [], |
| 242 | }; |
| 243 | |
| 244 | // Dart |
| 245 | export const Dart = { |
| 246 | name: "Dart", |
| 247 | topLevelKeywords: ["class", "import", "void", "enum"], |
| 248 | singleLineComment: "//", |
| 249 | endOfLine: [";"], |
| 250 | }; |
| 251 | |
| 252 | // Solidity |
| 253 | export const Solidity = { |
| 254 | name: "Solidity", |
| 255 | topLevelKeywords: [ |
| 256 | "contract", |
| 257 | "event", |
| 258 | "modifier", |
| 259 | "function", |
| 260 | "constructor", |
| 261 | "for", |
| 262 | "require", |
| 263 | "emit", |
| 264 | "interface", |
| 265 | "error", |
| 266 | "library", |
| 267 | "struct", |
| 268 | "enum", |
| 269 | "type", |
| 270 | ], |
| 271 | singleLineComment: "//", |
| 272 | endOfLine: [";"], |
| 273 | }; |
| 274 | |
| 275 | // Lua |
| 276 | export const Lua = { |
| 277 | name: "Lua", |
| 278 | topLevelKeywords: ["function"], |
| 279 | singleLineComment: "--", |
| 280 | endOfLine: [], |
| 281 | }; |
| 282 | |
| 283 | // YAML |
| 284 | export const YAML: AutocompleteLanguageInfo = { |
| 285 | name: "YAML", |
| 286 | topLevelKeywords: [], |
| 287 | singleLineComment: "#", |
| 288 | endOfLine: [], |
| 289 | lineFilters: [ |
| 290 | // Only display one list item at a time |
| 291 | async function* ({ lines, fullStop }) { |
| 292 | let seenListItem = false; |
| 293 | for await (const line of lines) { |
| 294 | if (line.trim().startsWith("- ")) { |
| 295 | if (seenListItem) { |
| 296 | fullStop(); |
| 297 | break; |
| 298 | } else { |
| 299 | seenListItem = true; |
| 300 | } |
| 301 | } |
| 302 | yield line; |
| 303 | } |
| 304 | }, |
| 305 | // Don't allow consecutive lines of same key |
| 306 | async function* ({ lines }) { |
| 307 | let lastKey = undefined; |
| 308 | for await (const line of lines) { |
| 309 | if (line.includes(":")) { |
| 310 | const key = line.split(":")[0]; |
| 311 | if (key === lastKey) { |
| 312 | break; |
| 313 | } else { |
| 314 | yield line; |
| 315 | lastKey = key; |
| 316 | } |
| 317 | } else { |
| 318 | yield line; |
| 319 | } |
| 320 | } |
| 321 | }, |
| 322 | ], |
| 323 | }; |
| 324 | |
| 325 | export const Json: AutocompleteLanguageInfo = { |
| 326 | name: "JSON", |
| 327 | topLevelKeywords: [], |
| 328 | singleLineComment: "//", |
| 329 | endOfLine: [",", "}", "]"], |
| 330 | charFilters: [ |
| 331 | function matchBrackets({ chars, prefix, suffix, filepath, multiline }) { |
| 332 | const bracketMatchingService = new BracketMatchingService(); |
| 333 | return bracketMatchingService.stopOnUnmatchedClosingBracket( |
| 334 | chars, |
| 335 | prefix, |
| 336 | suffix, |
| 337 | filepath, |
| 338 | multiline, |
| 339 | ); |
| 340 | }, |
| 341 | ], |
| 342 | }; |
| 343 | |
| 344 | export const Markdown: AutocompleteLanguageInfo = { |
| 345 | name: "Markdown", |
| 346 | topLevelKeywords: [], |
| 347 | singleLineComment: "", |
| 348 | endOfLine: [], |
| 349 | useMultiline: ({ prefix, suffix }) => { |
| 350 | const singleLineStarters = ["- ", "* ", /^\d+\. /, "> ", "```", /^#{1,6} /]; |
| 351 | let currentLine = prefix.split("\n").pop(); |
| 352 | if (!currentLine) { |
| 353 | return true; |
| 354 | } |
| 355 | currentLine = currentLine.trim(); |
| 356 | for (const starter of singleLineStarters) { |
| 357 | if ( |
| 358 | typeof starter === "string" |
| 359 | ? currentLine.startsWith(starter) |
| 360 | : starter.test(currentLine) |
| 361 | ) { |
| 362 | return false; |
| 363 | } |
| 364 | } |
| 365 | return true; |
| 366 | }, |
| 367 | }; |
| 368 | |
| 369 | export const LANGUAGES: { [extension: string]: AutocompleteLanguageInfo } = { |
| 370 | ts: Typescript, |
| 371 | js: JavaScript, |
| 372 | tsx: Typescript, |
| 373 | json: Json, |
| 374 | jsx: Typescript, |
| 375 | ipynb: Python, |
| 376 | py: Python, |
| 377 | pyi: Python, |
| 378 | java: Java, |
| 379 | cpp: Cpp, |
| 380 | cxx: Cpp, |
| 381 | h: Cpp, |
| 382 | hpp: Cpp, |
| 383 | cs: CSharp, |
| 384 | c: C, |
| 385 | scala: Scala, |
| 386 | sc: Scala, |
| 387 | go: Go, |
| 388 | rs: Rust, |
| 389 | hs: Haskell, |
| 390 | php: PHP, |
| 391 | rb: Ruby, |
| 392 | rails: RubyOnRails, |
| 393 | swift: Swift, |
| 394 | kt: Kotlin, |
| 395 | clj: Clojure, |
| 396 | cljs: Clojure, |
| 397 | cljc: Clojure, |
| 398 | jl: Julia, |
| 399 | fs: FSharp, |
| 400 | fsi: FSharp, |
| 401 | fsx: FSharp, |
| 402 | fsscript: FSharp, |
| 403 | r: R, |
| 404 | R: R, |
| 405 | dart: Dart, |
| 406 | sol: Solidity, |
| 407 | yaml: YAML, |
| 408 | yml: YAML, |
| 409 | md: Markdown, |
| 410 | lua: Lua, |
| 411 | luau: Lua, |
| 412 | }; |
| 413 | |
| 414 | export function languageForFilepath(fileUri: string): AutocompleteLanguageInfo { |
| 415 | const extension = getUriFileExtension(fileUri); |
| 416 | return LANGUAGES[extension] || Typescript; |
| 417 | } |