Skip to content

Commit 3bfd24e

Browse files
authored
Merge pull request #210 from MaxGyver83/hare
Add Hare support
2 parents cab1e63 + 26e3f84 commit 3bfd24e

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This currently works for various constructs in the following languages:
5252
- Eruby
5353
- Go
5454
- HAML
55+
- Hare
5556
- HTML (and HTML-like markup)
5657
- Handlebars
5758
- JSON

autoload/sj/hare.vim

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
let s:skip_syntax = sj#SkipSyntax(['String', 'Comment'])
2+
let s:eol_pattern = '\s*\%(//.*\)\=$'
3+
4+
function! sj#hare#SplitQuestionMark()
5+
if sj#SearchSkip('.?', s:skip_syntax, 'Wc', line('.')) <= 0
6+
return 0
7+
endif
8+
9+
let current_line = line('.')
10+
let end_col = col('.')
11+
let question_mark_col = col('.') + 1
12+
let char = getline('.')[end_col - 1]
13+
14+
let previous_start_col = -2
15+
let start_col = -1
16+
17+
while previous_start_col != start_col
18+
let previous_start_col = start_col
19+
20+
if char =~ '\k'
21+
call search('\k\+?;', 'bWc', line('.'))
22+
let start_col = col('.')
23+
elseif char == '}'
24+
" go to opening bracket
25+
normal! %
26+
let start_col = col('.')
27+
elseif char == ')'
28+
" go to opening bracket
29+
normal! %
30+
" find first method-call char
31+
call search('\%(\k\|\.\|::\)\+!\?(', 'bWc')
32+
33+
if line('.') != current_line
34+
" multiline expression, let's just ignore it
35+
return 0
36+
endif
37+
38+
let start_col = col('.')
39+
else
40+
break
41+
endif
42+
43+
if start_col <= 1
44+
" first character, no previous one
45+
break
46+
endif
47+
48+
" move backwards one step from the start
49+
let pos = getpos('.')
50+
let pos[2] = start_col - 1
51+
call setpos('.', pos)
52+
let char = getline('.')[col('.') - 1]
53+
endwhile
54+
55+
let expr = sj#GetCols(start_col, end_col)
56+
57+
let replacement = join([
58+
\ "match (".expr.") {",
59+
\ "case error => abort();",
60+
\ "case let t: type =>",
61+
\ " yield t;",
62+
\ "}"
63+
\ ], "\n")
64+
65+
call sj#ReplaceCols(start_col, question_mark_col, replacement)
66+
return 1
67+
endfunction

doc/splitjoin.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CONTENTS *splitjoin* *splitjoin-content
1717
Go.....................................: |splitjoin-go|
1818
HAML...................................: |splitjoin-haml|
1919
Handlebars.............................: |splitjoin-handlebars|
20+
Hare...................................: |splitjoin-hare|
2021
HTML...................................: |splitjoin-html|
2122
Java...................................: |splitjoin-java|
2223
Javascript.............................: |splitjoin-javascript| |splitjoin-json|
@@ -574,6 +575,53 @@ Block components ~
574575
{{/component-name}}
575576
<
576577

578+
==============================================================================
579+
HARE *splitjoin-hare*
580+
581+
Structs ~
582+
>
583+
struct { source: str, line: 1 };
584+
585+
struct {
586+
source: str,
587+
line: 1,
588+
};
589+
<
590+
Function definitions, calls, and arrays ~
591+
>
592+
fn function_def(values: []u8, s: str) void = {};
593+
fn function_def(
594+
values: []u8,
595+
s: str,
596+
) void = {};
597+
598+
function_call(values: []u8, s: str);
599+
function_call(
600+
values: []u8,
601+
s: str,
602+
);
603+
604+
let x = [1, 2, 3];
605+
let x = [
606+
1,
607+
2,
608+
3,
609+
];
610+
<
611+
Question mark operator ~
612+
>
613+
const num = getnumber()?;
614+
615+
const num = match (getnumber()) {
616+
case error => abort();
617+
case let t: type =>
618+
yield t;
619+
};
620+
<
621+
Note that, at the moment, only splitting question marks is supported, joining
622+
an appropriate match expression into a question-mark operator is not.
623+
624+
577625
==============================================================================
578626
HTML *splitjoin-html*
579627

ftplugin/hare/splitjoin.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let b:splitjoin_trailing_comma = 1
2+
3+
let b:splitjoin_split_callbacks = [
4+
\ 'sj#hare#SplitQuestionMark',
5+
\ 'sj#rust#SplitCurlyBrackets',
6+
\ 'sj#rust#SplitArray',
7+
\ 'sj#rust#SplitArgs',
8+
\ ]
9+
10+
let b:splitjoin_join_callbacks = [
11+
\ 'sj#rust#JoinCurlyBrackets',
12+
\ 'sj#rust#JoinArray',
13+
\ 'sj#rust#JoinArgs',
14+
\ ]

spec/plugin/hare_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'spec_helper'
2+
3+
describe "hare" do
4+
let(:filename) { 'test.ha' }
5+
6+
specify "question mark operator" do
7+
set_file_contents <<~EOF
8+
const num = getnumber()?;
9+
EOF
10+
11+
vim.search('getnumber')
12+
split
13+
14+
assert_file_contents <<~EOF
15+
const num = match (getnumber()) {
16+
case error => abort();
17+
case let t: type =>
18+
yield t;
19+
};
20+
EOF
21+
end
22+
end

0 commit comments

Comments
 (0)