Skip to content

Commit

Permalink
Update perl snippets (#460)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
surmish authored Jun 9, 2024
1 parent e11b09b commit 7801e3c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@
"language": "tcl",
"path": "./snippets/tcl.json"
},
{
"language": "perl",
"path": "./snippets/perl.json"
},
{
"language": "verilog",
"path": "./snippets/verilog.json"
Expand Down
74 changes: 41 additions & 33 deletions snippets/perl.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
},
"if statement": {
"prefix": "if",
"body": ["if ( ${1:condition} ) {", "\t${0:body}", "}"]
"body": ["if ( ${1:condition} ) {", "\t${2:body}", "}"]
},
"if else statement": {
"prefix": "ife",
"body": [
"if ( ${1:condition} ) {",
"\t${2:body}",
"} else {",
"\t${0:body}",
"\t${3:body}",
"}"
]
},
Expand All @@ -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": [
"################################################################################",
"#",
Expand All @@ -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",
Expand All @@ -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": {
Expand All @@ -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"
]
Expand Down

0 comments on commit 7801e3c

Please sign in to comment.