Skip to content

Commit

Permalink
Add some snippets to JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Aug 1, 2024
1 parent 1f2eebd commit b81a96b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
1 change: 1 addition & 0 deletions package/src/extensions/autocomplete/javascript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@ const completeScope =
export { jsxTagCompletion } from "./jsx.js"
export { completeKeywords } from "./keywords.js"
export { globalReactAttributes, reactTags } from "./reactData.js"
export { completeSnippets, jsSnipets } from "./snippets.js"
export { jsContext, completeScope }
4 changes: 2 additions & 2 deletions package/src/extensions/autocomplete/javascript/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JSContext } from "./index.js"
import { Completion, CompletionSource } from "../types.js"

const jsKeyWords: Completion[] =
"as,await,break,case,catch,class,const,continue,debugger,default,delete,do,else,export,extends,finally,for,function,if,import,in,instanceof,let,new,null,of,package,return,static,super,switch,this,throw,try,typeof,undefined,var,void,while,with,yield"
"as,await,break,case,catch,class,const,continue,debugger,default,delete,do,else,export,extends,false,finally,for,function,if,import,in,instanceof,let,new,null,of,package,return,static,super,switch,this,throw,true,try,typeof,undefined,var,void,while,with,yield"
.split(",")
.map(name => ({ label: name, icon: "keyword" }))

Expand All @@ -13,7 +13,7 @@ const tsKeywords: Completion[] = jsKeyWords.concat(
)

/**
* Completion source that adds auto completion for JS/TS keywords
* Completion source that adds autocompletion for JS/TS keywords
*/
const completeKeywords: CompletionSource<JSContext> = ({ path, explicit, language, pos }) => {
if (path?.length == 1 && (path[0] || explicit)) {
Expand Down
137 changes: 137 additions & 0 deletions package/src/extensions/autocomplete/javascript/snippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { JSContext } from "./index.js"
import { Completion, CompletionSource } from "../types.js"

const jsSnipets: Completion[] = [
{
label: "log",
insert: "console.log()",
tabStops: [12],
icon: "snippet",
detail: "Log to the console",
},
{
label: "warn",
insert: "console.warn()",
tabStops: [13],
icon: "snippet",
detail: "Log warning to the console",
},
{
label: "error",
insert: "console.error()",
tabStops: [14],
icon: "snippet",
detail: "Log error to the console",
},
{
label: "import",
insert: 'import { } from ""',
tabStops: [9],
icon: "snippet",
detail: "Import Statement",
},
{
label: "function",
insert: "function name() {\n\t\n}",
tabStops: [9, 13],
icon: "snippet",
detail: "Function Statement",
},
{
label: "class",
insert: "class name {\n\tconstructor() {\n\t\t\n\t}\n}",
tabStops: [6, 10],
icon: "snippet",
detail: "Class Definition",
},
{
label: "throw",
insert: 'throw new Error("")',
tabStops: [17],
icon: "snippet",
detail: "Throw Exception",
},
{
label: "trycatch",
insert: "try {\n\t\n} catch (error) {\n\t\n}",
tabStops: [7],
icon: "snippet",
detail: "Try-Catch Statement",
},
{
label: "for",
insert: "for (let index = 0; index < array.length; index++) {\n\t\n}",
tabStops: [54],
icon: "snippet",
detail: "For Loop",
},
{
label: "forof",
insert: "for (const item of iterable) {\n\t\n}",
tabStops: [32],
icon: "snippet",
detail: "For-Of Loop",
},
{
label: "forin",
insert: "for (const key in object) {\n\t\n}",
tabStops: [29],
icon: "snippet",
detail: "For-In Loop",
},
{
label: "forawaitof",
insert: "for await (const item of iterable) {\n\t\n}",
tabStops: [38],
icon: "snippet",
detail: "For-Await-Of Loop",
},
{
label: "if",
insert: "if () {\n\t\n}",
tabStops: [4],
icon: "snippet",
detail: "If Statement",
},
{
label: "ifelse",
insert: "if () {\n\t\n} else {\n\t\n}",
tabStops: [4],
icon: "snippet",
detail: "If-Else Statement",
},
{
label: "while",
insert: "while () {\n\t\n}",
tabStops: [7],
icon: "snippet",
detail: "While Statement",
},
{
label: "dowhile",
insert: "do {\n\t\n} while ()",
tabStops: [6],
icon: "snippet",
detail: "Do-While Statement",
},
{
label: "switch",
insert: "switch () {\n\t\n}",
tabStops: [8],
icon: "snippet",
detail: "Switch Statement",
},
]

const completeSnippets = (snippets: Completion[]): CompletionSource<JSContext> => {
return ({ path, explicit, pos }) => {
if (path?.length == 1 && (path[0] || explicit)) {
return {
from: pos - path[0].length,
options: snippets,
}
}
}
}

export { jsSnipets, completeSnippets }
3 changes: 3 additions & 0 deletions package/src/testsite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import { autoComplete, registerCompletions } from "../extensions/autocomplete"
import {
completeKeywords,
completeScope,
completeSnippets,
jsContext,
jsSnipets,
jsxTagCompletion,
} from "../extensions/autocomplete/javascript"
import { markupCompletion, htmlTags, globalHtmlAttributes } from "../extensions/autocomplete/markup"
Expand Down Expand Up @@ -235,6 +237,7 @@ registerCompletions(["javascript", "js", "jsx", "tsx", "typescript", "ts"], {
completeScope(window),
completeKeywords,
jsxTagCompletion(reactTags, globalReactAttributes),
completeSnippets(jsSnipets),
],
})

Expand Down

0 comments on commit b81a96b

Please # to comment.