Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Forked from bantya/USAGE
Last active June 5, 2024 19:45
Show Gist options
  • Save davidwebca/ffcadb027cfe016bea1b6c4b05f02614 to your computer and use it in GitHub Desktop.
Save davidwebca/ffcadb027cfe016bea1b6c4b05f02614 to your computer and use it in GitHub Desktop.
Convert sublime snippets to vscode.
<?php
$scopes = [
"source.abap" => "abap",
"source.ada" => "ada",
"source.agda" => "agda",
"source.al" => "al",
"source.ampl" => "ampl",
"source.ansible" => "ansible",
"source.apex" => "apex",
"source.arduino" => "arduino",
"source.assembly" => "assembly",
"source.asm" => "assembly",
"source.awk" => "awk",
"source.azcli" => "azcli",
"source.ballerina" => "ballerina",
"source.batch" => "batch",
"source.bicep" => "bicep",
"source.blade" => "blade",
"source.c" => "c",
"source.c++" => "cpp",
"source.c#" => "csharp",
"source.cabal" => "cabal",
"source.cadence" => "cadence",
"source.clojure" => "clojure",
"source.cmake" => "cmake",
"source.cobol" => "cobol",
"source.coffee" => "coffeescript",
"source.coldfusion" => "coldfusion",
"source.common-lisp" => "lisp",
"source.cpp" => "cpp",
"source.csharp" => "csharp",
"source.css" => "css",
"source.csv" => "csv",
"source.cue" => "cue",
"source.d" => "d",
"source.dart" => "dart",
"source.dataweave" => "dataweave",
"source.diff" => "diff",
"source.dockerfile" => "dockerfile",
"source.dot" => "dot",
"source.ebnf" => "ebnf",
"source.editorconfig" => "editorconfig",
"source.elixir" => "elixir",
"source.elm" => "elm",
"source.emacs-lisp" => "lisp",
"source.erb" => "erb",
"source.erlang" => "erlang",
"source.f#" => "fsharp",
"source.f90" => "fortran",
"source.forth" => "forth",
"source.fortran" => "fortran",
"source.fs" => "fsharp",
"source.gcode" => "gcode",
"source.gdscript" => "gdscript",
"source.gherkin" => "gherkin",
"source.go" => "go",
"source.graphql" => "graphql",
"source.groovy" => "groovy",
"source.haskell" => "haskell",
"source.haxe" => "haxe",
"source.hbs" => "hbs",
"source.html" => "html",
"source.html.vue" => "vue",
"source.ini" => "ini",
"source.java" => "java",
"source.js" => "javascript",
"source.javascript" => "javascript",
"source.javascriptreact" => "javascriptreact",
"source.json" => "json",
"source.julia" => "julia",
"source.kotlin" => "kotlin",
"source.latex" => "latex",
"source.lean" => "lean",
"source.less" => "less",
"source.liquid" => "liquid",
"source.lisp" => "lisp",
"source.lua" => "lua",
"source.makefile" => "makefile",
"source.markdown" => "markdown",
"source.matlab" => "matlab",
"source.mizar" => "mizar",
"source.mongodb" => "mongodb",
"source.msql" => "msql",
"source.nix" => "nix",
"source.objective-c" => "objectivec",
"source.objective-cpp" => "objectivecpp",
"source.ocaml" => "ocaml",
"source.pascal" => "pascal",
"source.perl" => "perl",
"source.php" => "php",
"source.powershell" => "powershell",
"source.protobuf" => "protobuf",
"source.pug" => "pug",
"source.python" => "python",
"source.q" => "q",
"source.r" => "r",
"source.racket" => "racket",
"source.razor" => "razor",
"source.re" => "re",
"source.reason" => "reason",
"source.red" => "red",
"source.regex" => "regex",
"source.ren'py" => "renpy",
"source.robotframework" => "robotframework",
"source.ruby" => "ruby",
"source.rust" => "rust",
"source.s" => "s",
"source.sas" => "sas",
"source.scala" => "scala",
"source.scheme" => "scheme",
"source.sass" => "sass",
"source.scss" => "scss",
"source.shaderlab" => "shaderlab",
"source.shellscript" => "shellscript",
"source.sh" => "shellscript",
"source.solidity" => "solidity",
"source.sql" => "sql",
"source.stylus" => "stylus",
"source.swift" => "swift",
"source.systemverilog" => "systemverilog",
"source.tcl" => "tcl",
"source.terraform" => "terraform",
"source.tex" => "tex",
"source.text" => "text",
"source.toml" => "toml",
"source.ts" => "typescript",
"source.typescript" => "typescript",
"source.typescriptreact" => "typescriptreact",
"source.vala" => "vala",
"source.vb.net" => "vb",
"source.verilog" => "verilog",
"source.vhdl" => "vhdl",
"source.viml" => "viml",
"source.vue" => "vue",
"source.wasm" => "wasm",
"source.wolfram" => "wolfram",
"source.xml" => "xml",
"source.yaml" => "yaml",
"source.yara" => "yara",
"source.zig" => "zig",
"text.html" => "html",
"text.html.basic" => "html",
"text.html.markdown" => "markdown",
"text.html.vue" => "vue",
"text.plain.null" => "plain text",
"text.plain.text" => "plain text",
"text.xml" => "xml",
"text.yaml" => "yaml",
"text.json" => "json",
"text.csv" => "csv",
"text.properties" => "properties",
"text.ini" => "ini",
"text.tex" => "tex",
"text.css" => "css",
"text.less" => "less",
"text.scss" => "scss",
];
$directory = $argv[1];
$directory = str_replace(['.', '/', '\\', '_', ' '], '', $directory);
$files = getFiles('./' . $directory . '/');
$output = [];
foreach ($files as $file) {
$content = getContent($file, $directory);
$trig = isset($content['trigger']) ? $content['trigger'] : '';
$scop = isset($content['scope']) ? $content['scope'] : '';
$scoplist = explode(',', $scop);
$scopArr = [];
foreach($scoplist as $scopa) {
$scoppp = trim($scopa);
$scoppequiv = isset($scopes[$scoppp]) ? $scopes[$scoppp] : '';
if(empty($scoppequiv) && !empty($scoppp)) {
echo "\e[41mError! The scope $scoppp was not found in the array. \e[0m
";
}
$scopArr[] = $scoppequiv;
}
$convScope = !empty($scopArr) ? implode(',', $scopArr) : '';
$output[$content['description']] = [
'prefix' => $trig,
'body' => $content['snippet'],
'description' => $content['description'],
'scope' => $convScope,
];
}
saveContent($directory . '.json', $output);
function getContent(string $file, string $directory)
{
$content = simplexml_load_file('./' . $directory . '/' . $file, 'SimpleXMLElement', LIBXML_NOCDATA);
$data = [];
if($content) {
$snippet = explode("\n", $content->content);
array_shift($snippet);
array_pop($snippet);
$description = getName($file, $directory);
$data['description'] = $description;
$data['snippet'] = $snippet;
if($trigger = $content->tabTrigger) {
$data['trigger'] = $content->tabTrigger->__toString();
}
if($scope = $content->scope) {
$data['scope'] = $content->scope->__toString();
}
}
return $data;
}
function getName(string $name, string $directory)
{
$name = explode('.', $name);
$name = $name[0];
$name = preg_replace('/[^\_\-\s\pN\pL]+/u', '', $name);
$name = trim(preg_replace('/[\_\-\s]+/', ' ', $name), '-');
// $name = $directory . ' ' . trim(str_replace($directory, '', $name));
$name = trim(str_replace($directory, '', $name));
return $name;
}
function saveContent($name, $output)
{
$output = json_encode($output, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
if (false !== file_put_contents('./' . $name, $output)) {
echo "\e[42mDone!\e[0m
";
} else {
echo "\e[41mError!\e[0m
";
}
}
function getFiles(string $directory)
{
return array_values(
array_filter(
array_map(
function ($file) {
if ($file === '.' || $file === '..') {
return false;
}
return $file;
},
scandir($directory)
)
)
);
}
php snippets.php [SNIPPETS_DIRECTORY]
e.g. php snippets.php vue
SNIPPETS_DIRECTORY: Assuming that the snippets are categorized in directories (preferably by languages) and snippet directories and the script have the same parent directory.
e.g.
/snippets
|
+--- snippets.php
|
+--- /php
| ...
+--- /sass
| ...
+--- /vue
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment