AndrewRadev/splitjoin.vim

Rust: grouped multiline imports do not split correctly

interruptinuse opened this issue · 4 comments

The plugin does not handle Rust's grouped imports correctly when they're multiline.

( is the cursor's position)

usecrate::{
	import1, import2
};

// Is split into
usecrate::import1;
use crate::import2;
	import1, import2
};

// Should be split into
usecrate::import1;
use crate::import2;

Joining first (which puts the imports on the same line), then splitting works as a workaround.

When splitting, the plugin mostly checks a single line and doesn't go farther than that. There might be some existing special cases (I think ruby's case statement breaks this principle), but I try to stick to "split one line, join multiple" as a rule. It's much easier to deal with the contents of a single line and much easier to say "this one line doesn't match a pattern, so let's move on".

In this particular case, it seems easy enough to handle the multiline case, and I can't help but admit it would be convenient for it to work -- I've hit similar situations myself. So I've applied a fix in the main branch. But if it turns out that this complicates something in the future, I might disable it and request that users join first, then split.

For now, though, it should be working and I can't see any failing tests. Could you try it out as well and let me know if you hit any new bugs?

Thanks!

I do hit a minor bug, yes; split imports seem to lose the trailing newline:

use █std::collections::{
	HashMap,
	HashSet
};
use std::error::Error;

// Is split into
use █std::collections::HashMap;
use std::collections::HashSet;use std::error::Error;


{
	use █std::collections::{
		HashMap,
		HashSet
	};
}

// Is split into
{
	use █std::collections::HashMap;
	use std::collections::HashSet;}

Pushed a fix. Try now?

WFM at 64f68a6, thanks!