Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xcode 8.3.3中格式化选中的多行代码,最后一行没有对齐 #107

Open
seigelions opened this issue Jun 12, 2017 · 3 comments
Open

Comments

@seigelions
Copy link

IDE: Xcode 8.3.3
OS: 10.12.5

多行赋值语句全部选中之后,使用快捷键对齐代码的时候
最后一行没有按照"="号对其

@KenmuHuang
Copy link

使用 XAlign.1.0.dmg 在 Xcode 9 beta 版本下,同样有这问题;请修复下。

@seigelions
Copy link
Author

IDE: Xcode 9 & 9.0.1
OS: 10.12.6
同样有这问题

@titman
Copy link

titman commented Feb 1, 2018

How to fix "The last line" problem.

  1. Open XAlign.xcodeproj
  2. Find and select SourceEditorCommand.m file
  3. Find and override these code
+ (void)autoAlign:(XCSourceEditorCommandInvocation *)invocation
{
	NSMutableArray * selections = [NSMutableArray array];
	
	for ( XCSourceTextRange *range in invocation.buffer.selections )
	{
		for ( NSInteger i = range.start.line; i < range.end.line ; i++)
		{
			[selections addObject:invocation.buffer.lines[i]];
		}
	}
	
	NSString * selectedString = [selections componentsJoinedByString:@""];
	
	NSArray * patternGroup = [XAlignPatternManager patternGroupMatchWithString:selectedString];
	
	if ( !patternGroup )
		return;
	
	NSString * alignedString = [selectedString stringByAligningWithPatterns:patternGroup];
	
	NSArray * result = [alignedString componentsSeparatedByString:@"\n"];
	
	for ( XCSourceTextRange *range in invocation.buffer.selections )
	{
		for ( NSInteger i = range.start.line, j=0; i < range.end.line ; i++, j++ )
		{
			invocation.buffer.lines[i] = result[j];
		}
	}
}

The new code.

+ (void)autoAlign:(XCSourceEditorCommandInvocation *)invocation
{
	NSMutableArray * selections = [NSMutableArray array];
    
	for ( XCSourceTextRange * range in invocation.buffer.selections )
	{
		for ( NSInteger i = range.start.line; i < range.end.line + 1 ; i++)
		{
         		if ( i <= invocation.buffer.lines.count )
          		{
         			 [selections addObject:invocation.buffer.lines[i]];
         		}
		}
	}
	
	NSString * selectedString = [selections componentsJoinedByString:@""];
	
	NSArray * patternGroup = [XAlignPatternManager patternGroupMatchWithString:selectedString];
	
	if ( !patternGroup )
		return;
	
	NSString * alignedString = [selectedString stringByAligningWithPatterns:patternGroup];
	
	NSArray * result = [alignedString componentsSeparatedByString:@"\n"];
	
	for ( XCSourceTextRange *range in invocation.buffer.selections )
	{
		for ( NSInteger i = range.start.line, j=0; i < range.end.line + 1 ; i++, j++ )
		{
         		if ( i <= invocation.buffer.lines.count )
         		{
                		invocation.buffer.lines[i] = result[j];
            		}
		}
	}
}
  1. Save and rebuild XAlign.xcodeproj

This was referenced Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants