From 7801e3cb6eb74cc18a68a1b4cd97219417be903d Mon Sep 17 00:00:00 2001 From: Urmish Shah <33273194+surmish@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:01:25 -0700 Subject: [PATCH] Update perl snippets (#460) * include perl in package.json to trigger perl snippets * fixed/updated some perl snippets * Escape $$ and $! occurrences * With native neovim snippets $0 is buggy Ok, apologies for multiple commits, but $0 placement is very buggy in neovim native snippets usage. The placeholder text for ${0:placeholder text} is not replaced when you actually start typing at that location. Instead of waiting for a fix in neovim, native-snippets.nvim or whatever tool, I'd rather just work around this in the snippets I'm writing. * fix join command --- package.json | 4 +++ snippets/perl.json | 74 +++++++++++++++++++++++++--------------------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index e5439d89..dc8d9d72 100644 --- a/package.json +++ b/package.json @@ -505,6 +505,10 @@ "language": "tcl", "path": "./snippets/tcl.json" }, + { + "language": "perl", + "path": "./snippets/perl.json" + }, { "language": "verilog", "path": "./snippets/verilog.json" diff --git a/snippets/perl.json b/snippets/perl.json index 4771e780..c45b5013 100644 --- a/snippets/perl.json +++ b/snippets/perl.json @@ -5,7 +5,7 @@ }, "if statement": { "prefix": "if", - "body": ["if ( ${1:condition} ) {", "\t${0:body}", "}"] + "body": ["if ( ${1:condition} ) {", "\t${2:body}", "}"] }, "if else statement": { "prefix": "ife", @@ -13,7 +13,7 @@ "if ( ${1:condition} ) {", "\t${2:body}", "} else {", - "\t${0:body}", + "\t${3:body}", "}" ] }, @@ -23,48 +23,48 @@ "if ( ${1:condition} ) {", "\t${2:body}", "} elsif ( ${3:condition} ) {", - "\t${0:body}", + "\t${3:body}", "}" ] }, "else statement": { "prefix": "else", - "body": ["else {", "\t${0:body}", "}"] + "body": ["else {", "\t${1:body}", "}"] }, "elsif statement": { "prefix": "elsif", - "body": ["elsif ( ${1:condition} ) {", "\t${0:body}", "}"] + "body": ["elsif ( ${1:condition} ) {", "\t${2:body}", "}"] }, "unless": { "prefix": "unless", - "body": ["unless ( ${1:condition} ) {", "\t${0:body}", "}"] + "body": ["unless ( ${1:condition} ) {", "\t${2:body}", "}"] }, "for loop": { "prefix": "for", "body": [ - "for (my $${1:loop var} = 0; $$1 < ${2:count}; $$1++) {", - "\t${0:body}", + "for (my \\$${1:loop var} = 0; \\$$1 < ${2:count}; \\$$1++) {", + "\t${3:body}", "}" ] }, "foreach loop": { "prefix": "foreach", - "body": ["foreach my $${1:element} ( @${2:array} ) {", "\t${0:body}", "}"] + "body": ["foreach my \\$${1:element} ( @${2:array} ) {", "\t${3:body}", "}"] }, "while Loop": { "prefix": "while", - "body": ["while ( ${1:condition} ) {", "\t${0:body}", "}"] + "body": ["while ( ${1:condition} ) {", "\t${2:body}", "}"] }, "do while Loop": { "prefix": "dowhile", - "body": ["do {", "\t${0:body}", "} while ( ${1:condition} );"] + "body": ["do {", "\t${2:body}", "} while ( ${1:condition} );"] }, "subroutine": { "prefix": "sub", - "body": ["sub ${1:subroutine name} {", "\t${0:sub body}", "}"] + "body": ["sub ${1:sub_name} {", "\t${2:body}", "}"] }, "Comment block for subroutine": { - "prefix": "#sub documentation", + "prefix": "documentation", "body": [ "################################################################################", "#", @@ -81,39 +81,40 @@ "open file to read": { "prefix": "openr", "body": [ - "open(my $${1:fh}, '<', \"${2:file name}\") or die \"Cannot open file '$2' for reading: $!\";" + "open(my \\$${1:fh}, '<', \"${2:file name}\") or die \"Cannot open file '$2' for reading: \\$!\";" ] }, "open file to write": { "prefix": "openw", "body": [ - "open(my $${1:fh}, '>', \"${2:file name}\") or die \"Cannot open file '$2' for writing: $!\";" + "open(my \\$${1:fh}, '>', \"${2:file name}\") or die \"Cannot open file '$2' for writing: \\$!\";" ] }, "print to file": { "prefix": "file print", - "body": ["print ${1:fh} \"$2\\n\";"] + "body": ["print \\$${1:fh} \"${1:string}\\n\";"] }, "read file into a scalar": { "prefix": "slurp", "body": [ - "local $/;", - "open(my $${1:fh}, '<', \"${2:file name}\") or die \"Cannot open file '$2' for reading: $!\";", - "my $${3:contents} = <$$1>;", - "close($$1);" + "use File::Slurp;", + "", + "my \\$${1:contents} = read_file(\"${2:file_path}\")" ] }, "read a directory": { "prefix": "readdir", "body": [ - "opendir(my $$dir, '$1') or die \"Cannot open directory '$1': $!\";", - "my @files = readdir($$dir);", - "closedir($$dir);" + "opendir(my \\$${1:dir}, '$2') or die \"Cannot open directory '$2': \\$!\";", + "my @files = readdir(\\$$1);", + "closedir(\\$$1);" ] }, "create a directory": { "prefix": "mkdir", - "body": ["mkdir \"${1:dir}\" or die \"Cannot create directory '$1': $!\";"] + "body": [ + "mkdir \"${1:dir}\" or die \"Cannot create directory '$1': \\$!\";" + ] }, "split a string": { "prefix": "split", @@ -124,7 +125,7 @@ "join array": { "prefix": "join", "body": [ - "my $${1:string var} = join('${2:delimiter pattern}', @${3:array var});" + "my \\$${1:string var} = join('${2:delimiter pattern}', @${3:array_var});" ] }, "format time": { @@ -138,29 +139,36 @@ "prefix": "trycatch", "body": [ "use Try::Tiny;", + "", "try {", - "\t${0:body}", + "\t${1:body}", "} catch {", - "\tmy $${1:err} = shift;", - "\tprint \"Error: $$1\\n\";", + "\tmy \\$catch_error = shift;", + "\tprint \"Error: \\$catch_error\\n\";", + "} finally {", + "\tif (@_) {", + "\t\tprint \"The try block died with error: @_\\n\"", + "\t} else {", + "\t\tprint \"The try ran successfully.\\n\"", + "\t}", "};" ] }, "perl module": { - "prefix": "perlmod", + "prefix": "module package", "body": [ "package ${1:ModuleName};", "use strict;", "use warnings;", "", "sub new {", - "\tmy ($$class, %args) = @_;", - "\tmy $$self = bless {%args}, $$class;", - "\treturn $$self;", + "\tmy (\\$class, %args) = @_;", + "\tmy \\$self = bless {%args}, \\$class;", + "\treturn \\$self;", "}", "", "# Add more methods here", - "$0", + "$2", "", "1; # Return true to indicate successful module loading" ]