Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Forked from bantya/USAGE
Last active June 5, 2024 19:45
Show Gist options
  • Select an option

  • Save davidwebca/ffcadb027cfe016bea1b6c4b05f02614 to your computer and use it in GitHub Desktop.

Select an option

Save davidwebca/ffcadb027cfe016bea1b6c4b05f02614 to your computer and use it in GitHub Desktop.

Revisions

  1. davidwebca revised this gist Jun 5, 2024. 1 changed file with 201 additions and 15 deletions.
    216 changes: 201 additions & 15 deletions snippets.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,163 @@
    <?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);

    @@ -9,10 +167,25 @@

    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' => $content['trigger'],
    'prefix' => $trig,
    'body' => $content['snippet'],
    'description' => $content['description'],
    'scope' => $convScope,
    ];
    }

    @@ -21,19 +194,29 @@
    function getContent(string $file, string $directory)
    {
    $content = simplexml_load_file('./' . $directory . '/' . $file, 'SimpleXMLElement', LIBXML_NOCDATA);
    $snippet = explode("\n", $content->content);
    $data = [];

    array_shift($snippet);
    array_pop($snippet);
    if($content) {
    $snippet = explode("\n", $content->content);

    $description = getName($file, $directory);
    $trigger = $content->tabTrigger->__toString();
    array_shift($snippet);
    array_pop($snippet);

    return [
    'description' => $description,
    'trigger' => $trigger,
    'snippet' => $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)
    @@ -42,7 +225,8 @@ function getName(string $name, string $directory)
    $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 = $directory . ' ' . trim(str_replace($directory, '', $name));
    $name = trim(str_replace($directory, '', $name));

    return $name;
    }
    @@ -52,9 +236,11 @@ 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";
    echo "\e[42mDone!\e[0m
    ";
    } else {
    echo "\e[41mError!\e[0m";
    echo "\e[41mError!\e[0m
    ";
    }
    }

    @@ -74,4 +260,4 @@ function ($file) {
    )
    )
    );
    }
    }
  2. @bantya bantya revised this gist Jul 23, 2018. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions USAGE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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
    ...

  3. @bantya bantya revised this gist Jul 23, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion snippets.php
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function getContent(string $file, string $directory)
    return [
    'description' => $description,
    'trigger' => $trigger,
    'snippet' => $snippet
    'snippet' => $snippet,
    ];
    }

    @@ -43,6 +43,7 @@ function getName(string $name, string $directory)
    $name = preg_replace('/[^\_\-\s\pN\pL]+/u', '', $name);
    $name = trim(preg_replace('/[\_\-\s]+/', ' ', $name), '-');
    $name = $directory . ' ' . trim(str_replace($directory, '', $name));

    return $name;
    }

  4. @bantya bantya revised this gist Jul 23, 2018. 1 changed file with 18 additions and 10 deletions.
    28 changes: 18 additions & 10 deletions snippets.php
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,32 @@
    <?php

    $files = getFiles('./php/');
    $directory = $argv[1];
    $directory = str_replace(['.', '/', '\\', '_', ' '], '', $directory);

    $files = getFiles('./' . $directory . '/');

    $output = [];

    foreach ($files as $file) {
    $content = getContent($file);
    $content = getContent($file, $directory);
    $output[$content['description']] = [
    'prefix' => $content['trigger'],
    'body' => $content['snippet'],
    'description' => $content['description'],
    ];
    }

    saveContent('php.snippets.json', $output);
    saveContent($directory . '.json', $output);

    function getContent(string $file) {
    $content = simplexml_load_file('./php/' . $file, 'SimpleXMLElement', LIBXML_NOCDATA);
    function getContent(string $file, string $directory)
    {
    $content = simplexml_load_file('./' . $directory . '/' . $file, 'SimpleXMLElement', LIBXML_NOCDATA);
    $snippet = explode("\n", $content->content);

    array_shift($snippet);
    array_pop($snippet);

    $description = getName($file);
    $description = getName($file, $directory);
    $trigger = $content->tabTrigger->__toString();

    return [
    @@ -31,16 +36,18 @@ function getContent(string $file) {
    ];
    }

    function getName(string $name) {
    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 = 'php ' . trim(str_replace('php', '', $name));
    $name = $directory . ' ' . trim(str_replace($directory, '', $name));
    return $name;
    }

    function saveContent($name, $output) {
    function saveContent($name, $output)
    {
    $output = json_encode($output, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

    if (false !== file_put_contents('./' . $name, $output)) {
    @@ -50,7 +57,8 @@ function saveContent($name, $output) {
    }
    }

    function getFiles(string $directory) {
    function getFiles(string $directory)
    {
    return array_values(
    array_filter(
    array_map(
  5. @bantya bantya created this gist Jul 23, 2018.
    68 changes: 68 additions & 0 deletions snippets.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <?php

    $files = getFiles('./php/');
    $output = [];

    foreach ($files as $file) {
    $content = getContent($file);
    $output[$content['description']] = [
    'prefix' => $content['trigger'],
    'body' => $content['snippet'],
    'description' => $content['description'],
    ];
    }

    saveContent('php.snippets.json', $output);

    function getContent(string $file) {
    $content = simplexml_load_file('./php/' . $file, 'SimpleXMLElement', LIBXML_NOCDATA);
    $snippet = explode("\n", $content->content);

    array_shift($snippet);
    array_pop($snippet);

    $description = getName($file);
    $trigger = $content->tabTrigger->__toString();

    return [
    'description' => $description,
    'trigger' => $trigger,
    'snippet' => $snippet
    ];
    }

    function getName(string $name) {
    $name = explode('.', $name);
    $name = $name[0];
    $name = preg_replace('/[^\_\-\s\pN\pL]+/u', '', $name);
    $name = trim(preg_replace('/[\_\-\s]+/', ' ', $name), '-');
    $name = 'php ' . trim(str_replace('php', '', $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)
    )
    )
    );
    }