diff --git a/ClangFormat.xcodeproj/project.pbxproj b/ClangFormat.xcodeproj/project.pbxproj index 831673d..f7b3fcf 100644 --- a/ClangFormat.xcodeproj/project.pbxproj +++ b/ClangFormat.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ 32ABEDDD187EFA4F00D4E980 /* TRVSPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 32ABEDDB187EFA4F00D4E980 /* TRVSPreferences.m */; }; 32D2236C187C28E1008D12A3 /* clang-format in Resources */ = {isa = PBXBuildFile; fileRef = 32D2236B187C28E1008D12A3 /* clang-format */; }; 32F745E418823A57007541E1 /* NSDocument+TRVSClangFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F745E318823A57007541E1 /* NSDocument+TRVSClangFormat.m */; }; + 5DDD748D1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DDD748C1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -49,6 +50,8 @@ 32D2236B187C28E1008D12A3 /* clang-format */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = "clang-format"; path = "bin/clang-format"; sourceTree = SOURCE_ROOT; }; 32F745E218823A57007541E1 /* NSDocument+TRVSClangFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDocument+TRVSClangFormat.h"; sourceTree = ""; }; 32F745E318823A57007541E1 /* NSDocument+TRVSClangFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDocument+TRVSClangFormat.m"; sourceTree = ""; }; + 5DDD748B1925B38E0020CAC6 /* NSTask+TRVSClangFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTask+TRVSClangFormat.h"; sourceTree = ""; }; + 5DDD748C1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTask+TRVSClangFormat.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -114,6 +117,8 @@ 32ABEDC9187EF90500D4E980 /* TRVSXcode.m */, 32F745E218823A57007541E1 /* NSDocument+TRVSClangFormat.h */, 32F745E318823A57007541E1 /* NSDocument+TRVSClangFormat.m */, + 5DDD748B1925B38E0020CAC6 /* NSTask+TRVSClangFormat.h */, + 5DDD748C1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m */, 32A57EEA187C05A4002DEC9D /* Supporting Files */, ); path = ClangFormat; @@ -208,6 +213,7 @@ 32A57EF1187C05A4002DEC9D /* TRVSClangFormat.m in Sources */, 3228E445187F200D0037DECF /* TRVSCodeFragment.m in Sources */, 32ABEDDC187EFA4F00D4E980 /* TRVSFormatter.m in Sources */, + 5DDD748D1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m in Sources */, 32F745E418823A57007541E1 /* NSDocument+TRVSClangFormat.m in Sources */, 32ABEDCA187EF90500D4E980 /* TRVSXcode.m in Sources */, 32ABEDDD187EFA4F00D4E980 /* TRVSPreferences.m in Sources */, diff --git a/ClangFormat/NSTask+TRVSClangFormat.h b/ClangFormat/NSTask+TRVSClangFormat.h new file mode 100644 index 0000000..af1346e --- /dev/null +++ b/ClangFormat/NSTask+TRVSClangFormat.h @@ -0,0 +1,15 @@ +// +// NSTask+TRVSClangFormat.h +// ClangFormat +// +// Created by Seth Delackner on 5/16/14. +// Copyright (c) 2014 Travis Jeffery. All rights reserved. +// + +#import + +@interface NSTask (TRVSClangFormat) + +- (void) killIfNotDoneBy:(NSDate *) killDate; + +@end diff --git a/ClangFormat/NSTask+TRVSClangFormat.m b/ClangFormat/NSTask+TRVSClangFormat.m new file mode 100644 index 0000000..924d271 --- /dev/null +++ b/ClangFormat/NSTask+TRVSClangFormat.m @@ -0,0 +1,23 @@ +// +// NSTask+TRVSClangFormat.m +// ClangFormat +// +// Created by Seth Delackner on 5/16/14. +// Copyright (c) 2014 Travis Jeffery. All rights reserved. +// + +#import "NSTask+TRVSClangFormat.h" + +@implementation NSTask (TRVSClangFormat) + +- (void) killIfNotDoneBy:(NSDate *) killDate { + while ([self isRunning]) { + if ([[NSDate date] laterDate:killDate] != killDate) { + NSLog(@"Error: task took too long. killing."); + [self terminate]; + } + [NSThread sleepForTimeInterval:1.0]; + } +} + +@end diff --git a/ClangFormat/TRVSCodeFragment.m b/ClangFormat/TRVSCodeFragment.m index 2a79f71..4c4251a 100644 --- a/ClangFormat/TRVSCodeFragment.m +++ b/ClangFormat/TRVSCodeFragment.m @@ -7,6 +7,7 @@ // #import "TRVSCodeFragment.h" +#import "NSTask+TRVSClangFormat.h" @interface TRVSCodeFragment () @@ -65,8 +66,9 @@ - (void)formatWithStyle:(NSString *)style [outputPipe.fileHandleForReading readToEndOfFileInBackgroundAndNotify]; + NSDate *killDate = [NSDate dateWithTimeIntervalSinceNow:10.0]; [task launch]; - [task waitUntilExit]; + [task killIfNotDoneBy:killDate]; NSData *errorData = [errorPipe.fileHandleForReading readDataToEndOfFile]; diff --git a/ClangFormat/TRVSFormatter.m b/ClangFormat/TRVSFormatter.m index 286cc15..859f81e 100644 --- a/ClangFormat/TRVSFormatter.m +++ b/ClangFormat/TRVSFormatter.m @@ -10,6 +10,7 @@ #import "TRVSXcode.h" #import "TRVSCodeFragment.h" #import "NSDocument+TRVSClangFormat.h" +#import "NSTask+TRVSClangFormat.h" @interface TRVSFormatter () @@ -221,8 +222,10 @@ - (void)fragmentsOfContinuousLineRanges:(NSArray *)continuousLineRanges task.launchPath = shellString; task.arguments = @[ @"-l", @"-c", @"which clang-format" ]; + NSDate *killDate = [NSDate dateWithTimeIntervalSinceNow:10.0]; [task launch]; - [task waitUntilExit]; + [task killIfNotDoneBy: killDate]; + [errorPipe.fileHandleForReading readDataToEndOfFile]; NSData *outputData = [outputPipe.fileHandleForReading readDataToEndOfFile]; NSString *outputPath = [[NSString alloc] initWithData:outputData