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

Change the Format File in Focus command interactive style to eclipse code format way #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ClangFormat/TRVSClangFormat.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ - (void)addActioningMenuItemsToFormatMenu {
keyEquivalent:@""];
[formatSelectedFilesItem setTarget:self.formatter];
[self.formatMenu addItem:formatSelectedFilesItem];

NSMenuItem *deleteLineItem =
[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Delete Line", nil)
action:@selector(deleteLine)
keyEquivalent:@""];
[deleteLineItem setTarget:self.formatter];
[self.formatMenu addItem:deleteLineItem];
}

- (void)addSeparatorToFormatMenu {
Expand Down
2 changes: 2 additions & 0 deletions ClangFormat/TRVSFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
- (void)formatSelectedFiles;
- (void)formatDocument:(IDESourceCodeDocument *)document;

- (void)deleteLine;

@end
20 changes: 13 additions & 7 deletions ClangFormat/TRVSFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ - (void)formatSelectedCharacters {
inDocument:[TRVSXcode sourceCodeDocument]];
}

- (void)deleteLine {
[[TRVSXcode textView] selectLine:nil];
[[TRVSXcode textView] delete:nil];
}

- (void)formatSelectedFiles {
NSArray *fileNavigableItems = [TRVSXcode selectedFileNavigableItems];

Expand Down Expand Up @@ -106,6 +111,8 @@ - (void)formatDocument:(IDESourceCodeDocument *)document {
- (void)formatRanges:(NSArray *)ranges
inDocument:(IDESourceCodeDocument *)document {
DVTSourceTextStorage *textStorage = [document textStorage];

NSRange originSelectedRange = [[TRVSXcode textView] selectedRange];

NSArray *lineRanges =
[self lineRangesOfCharacterRanges:ranges usingTextStorage:textStorage];
Expand All @@ -118,13 +125,12 @@ - (void)formatRanges:(NSArray *)ranges
usingTextStorage:textStorage
withDocument:document];

NSArray *selectionRanges =
[self selectionRangesAfterReplacingFragments:fragments
usingTextStorage:textStorage
withDocument:document];

if (selectionRanges.count > 0)
[[TRVSXcode textView] setSelectedRanges:selectionRanges];
[self selectionRangesAfterReplacingFragments:fragments
usingTextStorage:textStorage
withDocument:document];

[[TRVSXcode textView] setSelectedRange:originSelectedRange];
[[TRVSXcode textView] centerSelectionInVisibleArea:nil];
}

- (NSArray *)
Expand Down