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