Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This currently works for various constructs in the following languages:
- Eruby
- Go
- HAML
- Hare
- HTML (and HTML-like markup)
- Handlebars
- JSON
Expand Down
67 changes: 67 additions & 0 deletions autoload/sj/hare.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
let s:skip_syntax = sj#SkipSyntax(['String', 'Comment'])
let s:eol_pattern = '\s*\%(//.*\)\=$'

function! sj#hare#SplitQuestionMark()
if sj#SearchSkip('.?', s:skip_syntax, 'Wc', line('.')) <= 0
return 0
endif

let current_line = line('.')
let end_col = col('.')
let question_mark_col = col('.') + 1
let char = getline('.')[end_col - 1]

let previous_start_col = -2
let start_col = -1

while previous_start_col != start_col
let previous_start_col = start_col

if char =~ '\k'
call search('\k\+?;', 'bWc', line('.'))
let start_col = col('.')
elseif char == '}'
" go to opening bracket
normal! %
let start_col = col('.')
elseif char == ')'
" go to opening bracket
normal! %
" find first method-call char
call search('\%(\k\|\.\|::\)\+!\?(', 'bWc')

if line('.') != current_line
" multiline expression, let's just ignore it
return 0
endif

let start_col = col('.')
else
break
endif

if start_col <= 1
" first character, no previous one
break
endif

" move backwards one step from the start
let pos = getpos('.')
let pos[2] = start_col - 1
call setpos('.', pos)
let char = getline('.')[col('.') - 1]
endwhile

let expr = sj#GetCols(start_col, end_col)

let replacement = join([
\ "match (".expr.") {",
\ "case error => abort();",
\ "case let t: type =>",
\ " yield t;",
\ "}"
\ ], "\n")

call sj#ReplaceCols(start_col, question_mark_col, replacement)
return 1
endfunction
48 changes: 48 additions & 0 deletions doc/splitjoin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CONTENTS *splitjoin* *splitjoin-content
Go.....................................: |splitjoin-go|
HAML...................................: |splitjoin-haml|
Handlebars.............................: |splitjoin-handlebars|
Hare...................................: |splitjoin-hare|
HTML...................................: |splitjoin-html|
Java...................................: |splitjoin-java|
Javascript.............................: |splitjoin-javascript| |splitjoin-json|
Expand Down Expand Up @@ -574,6 +575,53 @@ Block components ~
{{/component-name}}
<

==============================================================================
HARE *splitjoin-hare*

Structs ~
>
struct { source: str, line: 1 };

struct {
source: str,
line: 1,
};
<
Function definitions, calls, and arrays ~
>
fn function_def(values: []u8, s: str) void = {};
fn function_def(
values: []u8,
s: str,
) void = {};

function_call(values: []u8, s: str);
function_call(
values: []u8,
s: str,
);

let x = [1, 2, 3];
let x = [
1,
2,
3,
];
<
Question mark operator ~
>
const num = getnumber()?;

const num = match (getnumber()) {
case error => abort();
case let t: type =>
yield t;
};
<
Note that, at the moment, only splitting question marks is supported, joining
an appropriate match expression into a question-mark operator is not.


==============================================================================
HTML *splitjoin-html*

Expand Down
14 changes: 14 additions & 0 deletions ftplugin/hare/splitjoin.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let b:splitjoin_trailing_comma = 1

let b:splitjoin_split_callbacks = [
\ 'sj#hare#SplitQuestionMark',
\ 'sj#rust#SplitCurlyBrackets',
\ 'sj#rust#SplitArray',
\ 'sj#rust#SplitArgs',
\ ]

let b:splitjoin_join_callbacks = [
\ 'sj#rust#JoinCurlyBrackets',
\ 'sj#rust#JoinArray',
\ 'sj#rust#JoinArgs',
\ ]
22 changes: 22 additions & 0 deletions spec/plugin/hare_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe "hare" do
let(:filename) { 'test.ha' }

specify "question mark operator" do
set_file_contents <<~EOF
const num = getnumber()?;
EOF

vim.search('getnumber')
split

assert_file_contents <<~EOF
const num = match (getnumber()) {
case error => abort();
case let t: type =>
yield t;
};
EOF
end
end