mirror of
https://github.com/ypresto/SwiftLintXcode.git
synced 2026-03-25 08:55:51 +00:00
Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
19 changed files with 259 additions and 360 deletions
|
|
@ -1,4 +0,0 @@
|
|||
disabled_rules:
|
||||
- line_length
|
||||
- force_cast
|
||||
- todo
|
||||
25
CHANGELOG.md
25
CHANGELOG.md
|
|
@ -1,31 +1,6 @@
|
|||
Change Log
|
||||
==========
|
||||
|
||||
Version 0.2.1
|
||||
----------------------------
|
||||
|
||||
- Added UUIDs for Xcode <=8.3.
|
||||
|
||||
|
||||
Version 0.2.0
|
||||
----------------------------
|
||||
|
||||
- Xcode 8 Support.
|
||||
|
||||
|
||||
Version 0.1.2
|
||||
----------------------------
|
||||
|
||||
- Fixed bug that saving non-swift file runs autocorrect.
|
||||
- Set working directory to where workspace (or project) file is placed.
|
||||
|
||||
|
||||
Version 0.1.1
|
||||
----------------------------
|
||||
|
||||
- Rename plug-in name: SwiftLintAutoCorrect -> SwiftLintXcode.
|
||||
|
||||
|
||||
Version 0.1.0
|
||||
----------------------------
|
||||
|
||||
|
|
|
|||
32
README.md
32
README.md
|
|
@ -1,39 +1,21 @@
|
|||
SwiftLintXcode
|
||||
==============
|
||||
SwiftLintAutoCorrect
|
||||
====================
|
||||
|
||||
An Xcode plug-in to format your code using [SwiftLint](https://github.com/realm/SwiftLint).
|
||||
|
||||
Runs `swiftlint autocorrect --path CURRENT_FILE` before \*.swift file is saved.
|
||||
|
||||

|
||||
|
||||
|
||||
IMPORTANT: Xcode 8 Installation
|
||||
-------------------------------
|
||||
|
||||
Xcode 8 won't load any unsigned plugins without resigning Xcode itself.
|
||||
https://github.com/alcatraz/Alcatraz/issues/475
|
||||
|
||||
See XVim's nice and simple installation doc to resign it..! (NOTE: AT YOUR OWN SECURITY RISK)
|
||||
https://github.com/XVimProject/XVim/blob/3167408ade82cfef87acc704822da61af69688f8/INSTALL_Xcode8.md
|
||||
|
||||
|
||||
INSTALLATION
|
||||
------------
|
||||
|
||||
Install via [Alcatraz](https://github.com/alcatraz/Alcatraz), a package manager for Xcode.
|
||||
|
||||
This plugin does not bundle swiftlint binary. Please ensure swiftlint is on PATH.
|
||||
|
||||
```bash
|
||||
# This plugin does not bundle swiftlint binary.
|
||||
# Please ensure swiftlint is on PATH.
|
||||
brew update && brew install swiftlint
|
||||
```
|
||||
|
||||
### Manual installation
|
||||
|
||||
```bash
|
||||
git clone https://github.com/ypresto/SwiftLintXcode
|
||||
cd SwiftLintXcode
|
||||
git clone https://github.com/ypresto/SwiftLintAutoCorrect
|
||||
cd SwiftLintAutoCorrect
|
||||
# Build and install.
|
||||
xcodebuild -configuration Release
|
||||
```
|
||||
|
|
@ -41,7 +23,7 @@ xcodebuild -configuration Release
|
|||
To uninstall, just remove plug-in directory.
|
||||
|
||||
```bash
|
||||
rm -rf "$HOME/Library/Application Support/Developer/Shared/Xcode/Plug-ins/SwiftLintXcode.xcplugin"
|
||||
rm -rf "$HOME/Library/Application Support/Developer/Shared/Xcode/Plug-ins/SwiftLintAutoCorrect.xcplugin"
|
||||
```
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,27 +7,22 @@
|
|||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
0498717F1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0498717E1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m */; };
|
||||
0498717F1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0498717E1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.m */; };
|
||||
049871811CB28EBF00C5F7B5 /* Formatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049871801CB28EBF00C5F7B5 /* Formatter.swift */; };
|
||||
04DFAD471CB508D1007998DF /* SwiftLintXcodeIDEHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 04DFAD461CB508D1007998DF /* SwiftLintXcodeIDEHelper.m */; };
|
||||
04DFAD491CB50BC8007998DF /* errorHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04DFAD481CB50BC8007998DF /* errorHelper.swift */; };
|
||||
04E4BF341CB25D3200BC7305 /* SwiftLintXcode.xcscheme in Resources */ = {isa = PBXBuildFile; fileRef = 04E4BF331CB25D3200BC7305 /* SwiftLintXcode.xcscheme */; };
|
||||
04E4BF361CB25D3200BC7305 /* SwiftLintXcode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E4BF351CB25D3200BC7305 /* SwiftLintXcode.swift */; };
|
||||
04E4BF341CB25D3200BC7305 /* SwiftLintAutoCorrect.xcscheme in Resources */ = {isa = PBXBuildFile; fileRef = 04E4BF331CB25D3200BC7305 /* SwiftLintAutoCorrect.xcscheme */; };
|
||||
04E4BF361CB25D3200BC7305 /* SwiftLintAutoCorrect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E4BF351CB25D3200BC7305 /* SwiftLintAutoCorrect.swift */; };
|
||||
04E4BF381CB25D3200BC7305 /* NSObject_Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E4BF371CB25D3200BC7305 /* NSObject_Extension.swift */; };
|
||||
04E4BF411CB273A700BC7305 /* SaveHook.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E4BF401CB273A700BC7305 /* SaveHook.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
0498717C1CB27F8800C5F7B5 /* SwiftLintXcode-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SwiftLintXcode-Bridging-Header.h"; sourceTree = "<group>"; };
|
||||
0498717D1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftLintXcodeTRVSXcode.h; sourceTree = "<group>"; };
|
||||
0498717E1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwiftLintXcodeTRVSXcode.m; sourceTree = "<group>"; };
|
||||
0498717C1CB27F8800C5F7B5 /* SwiftLintAutoCorrect-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SwiftLintAutoCorrect-Bridging-Header.h"; sourceTree = "<group>"; };
|
||||
0498717D1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftLintAutoCorrectTRVSXcode.h; sourceTree = "<group>"; };
|
||||
0498717E1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwiftLintAutoCorrectTRVSXcode.m; sourceTree = "<group>"; };
|
||||
049871801CB28EBF00C5F7B5 /* Formatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Formatter.swift; sourceTree = "<group>"; };
|
||||
04DFAD451CB508D1007998DF /* SwiftLintXcodeIDEHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftLintXcodeIDEHelper.h; sourceTree = "<group>"; };
|
||||
04DFAD461CB508D1007998DF /* SwiftLintXcodeIDEHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwiftLintXcodeIDEHelper.m; sourceTree = "<group>"; };
|
||||
04DFAD481CB50BC8007998DF /* errorHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = errorHelper.swift; sourceTree = "<group>"; };
|
||||
04E4BF2F1CB25D3100BC7305 /* SwiftLintXcode.xcplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftLintXcode.xcplugin; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
04E4BF331CB25D3200BC7305 /* SwiftLintXcode.xcscheme */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = SwiftLintXcode.xcscheme; path = SwiftLintXcode.xcodeproj/xcshareddata/xcschemes/SwiftLintXcode.xcscheme; sourceTree = SOURCE_ROOT; };
|
||||
04E4BF351CB25D3200BC7305 /* SwiftLintXcode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftLintXcode.swift; sourceTree = "<group>"; };
|
||||
04E4BF2F1CB25D3100BC7305 /* SwiftLintAutoCorrect.xcplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftLintAutoCorrect.xcplugin; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
04E4BF331CB25D3200BC7305 /* SwiftLintAutoCorrect.xcscheme */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = SwiftLintAutoCorrect.xcscheme; path = SwiftLintAutoCorrect.xcodeproj/xcshareddata/xcschemes/SwiftLintAutoCorrect.xcscheme; sourceTree = SOURCE_ROOT; };
|
||||
04E4BF351CB25D3200BC7305 /* SwiftLintAutoCorrect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftLintAutoCorrect.swift; sourceTree = "<group>"; };
|
||||
04E4BF371CB25D3200BC7305 /* NSObject_Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSObject_Extension.swift; sourceTree = "<group>"; };
|
||||
04E4BF391CB25D3200BC7305 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
04E4BF401CB273A700BC7305 /* SaveHook.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SaveHook.swift; sourceTree = "<group>"; };
|
||||
|
|
@ -37,7 +32,7 @@
|
|||
04E4BF271CB25D3100BC7305 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04E4BF311CB25D3200BC7305 /* SwiftLintXcode */,
|
||||
04E4BF311CB25D3200BC7305 /* SwiftLintAutoCorrect */,
|
||||
04E4BF301CB25D3100BC7305 /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -45,34 +40,31 @@
|
|||
04E4BF301CB25D3100BC7305 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04E4BF2F1CB25D3100BC7305 /* SwiftLintXcode.xcplugin */,
|
||||
04E4BF2F1CB25D3100BC7305 /* SwiftLintAutoCorrect.xcplugin */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
04E4BF311CB25D3200BC7305 /* SwiftLintXcode */ = {
|
||||
04E4BF311CB25D3200BC7305 /* SwiftLintAutoCorrect */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
0498717D1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.h */,
|
||||
0498717E1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m */,
|
||||
04DFAD451CB508D1007998DF /* SwiftLintXcodeIDEHelper.h */,
|
||||
04DFAD461CB508D1007998DF /* SwiftLintXcodeIDEHelper.m */,
|
||||
04E4BF351CB25D3200BC7305 /* SwiftLintXcode.swift */,
|
||||
0498717D1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.h */,
|
||||
0498717E1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.m */,
|
||||
04E4BF351CB25D3200BC7305 /* SwiftLintAutoCorrect.swift */,
|
||||
04E4BF371CB25D3200BC7305 /* NSObject_Extension.swift */,
|
||||
04E4BF401CB273A700BC7305 /* SaveHook.swift */,
|
||||
049871801CB28EBF00C5F7B5 /* Formatter.swift */,
|
||||
04DFAD481CB50BC8007998DF /* errorHelper.swift */,
|
||||
04E4BF391CB25D3200BC7305 /* Info.plist */,
|
||||
04E4BF321CB25D3200BC7305 /* Supporting Files */,
|
||||
0498717C1CB27F8800C5F7B5 /* SwiftLintXcode-Bridging-Header.h */,
|
||||
0498717C1CB27F8800C5F7B5 /* SwiftLintAutoCorrect-Bridging-Header.h */,
|
||||
);
|
||||
path = SwiftLintXcode;
|
||||
path = SwiftLintAutoCorrect;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
04E4BF321CB25D3200BC7305 /* Supporting Files */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04E4BF331CB25D3200BC7305 /* SwiftLintXcode.xcscheme */,
|
||||
04E4BF331CB25D3200BC7305 /* SwiftLintAutoCorrect.xcscheme */,
|
||||
);
|
||||
name = "Supporting Files";
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -80,9 +72,9 @@
|
|||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
04E4BF2E1CB25D3100BC7305 /* SwiftLintXcode */ = {
|
||||
04E4BF2E1CB25D3100BC7305 /* SwiftLintAutoCorrect */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 04E4BF3C1CB25D3200BC7305 /* Build configuration list for PBXNativeTarget "SwiftLintXcode" */;
|
||||
buildConfigurationList = 04E4BF3C1CB25D3200BC7305 /* Build configuration list for PBXNativeTarget "SwiftLintAutoCorrect" */;
|
||||
buildPhases = (
|
||||
04E4BF2C1CB25D3100BC7305 /* Sources */,
|
||||
04E4BF2D1CB25D3100BC7305 /* Resources */,
|
||||
|
|
@ -91,9 +83,9 @@
|
|||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = SwiftLintXcode;
|
||||
productName = SwiftLintXcode;
|
||||
productReference = 04E4BF2F1CB25D3100BC7305 /* SwiftLintXcode.xcplugin */;
|
||||
name = SwiftLintAutoCorrect;
|
||||
productName = SwiftLintAutoCorrect;
|
||||
productReference = 04E4BF2F1CB25D3100BC7305 /* SwiftLintAutoCorrect.xcplugin */;
|
||||
productType = "com.apple.product-type.bundle";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
|
@ -103,16 +95,15 @@
|
|||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 0730;
|
||||
LastUpgradeCheck = 0800;
|
||||
LastUpgradeCheck = 0730;
|
||||
ORGANIZATIONNAME = "Yuya Tanaka";
|
||||
TargetAttributes = {
|
||||
04E4BF2E1CB25D3100BC7305 = {
|
||||
CreatedOnToolsVersion = 7.3;
|
||||
LastSwiftMigration = 0800;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 04E4BF2B1CB25D3100BC7305 /* Build configuration list for PBXProject "SwiftLintXcode" */;
|
||||
buildConfigurationList = 04E4BF2B1CB25D3100BC7305 /* Build configuration list for PBXProject "SwiftLintAutoCorrect" */;
|
||||
compatibilityVersion = "Xcode 3.2";
|
||||
developmentRegion = English;
|
||||
hasScannedForEncodings = 0;
|
||||
|
|
@ -124,7 +115,7 @@
|
|||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
04E4BF2E1CB25D3100BC7305 /* SwiftLintXcode */,
|
||||
04E4BF2E1CB25D3100BC7305 /* SwiftLintAutoCorrect */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
|
@ -134,7 +125,7 @@
|
|||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
04E4BF341CB25D3200BC7305 /* SwiftLintXcode.xcscheme in Resources */,
|
||||
04E4BF341CB25D3200BC7305 /* SwiftLintAutoCorrect.xcscheme in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
@ -145,11 +136,9 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
0498717F1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m in Sources */,
|
||||
04E4BF361CB25D3200BC7305 /* SwiftLintXcode.swift in Sources */,
|
||||
0498717F1CB27F8900C5F7B5 /* SwiftLintAutoCorrectTRVSXcode.m in Sources */,
|
||||
04E4BF361CB25D3200BC7305 /* SwiftLintAutoCorrect.swift in Sources */,
|
||||
049871811CB28EBF00C5F7B5 /* Formatter.swift in Sources */,
|
||||
04DFAD471CB508D1007998DF /* SwiftLintXcodeIDEHelper.m in Sources */,
|
||||
04DFAD491CB50BC8007998DF /* errorHelper.swift in Sources */,
|
||||
04E4BF411CB273A700BC7305 /* SaveHook.swift in Sources */,
|
||||
04E4BF381CB25D3200BC7305 /* NSObject_Extension.swift in Sources */,
|
||||
);
|
||||
|
|
@ -172,10 +161,8 @@
|
|||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
|
|
@ -215,10 +202,8 @@
|
|||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
|
|
@ -234,27 +219,25 @@
|
|||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
04E4BF3D1CB25D3200BC7305 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
COMBINE_HIDPI_IMAGES = YES;
|
||||
DEPLOYMENT_LOCATION = YES;
|
||||
DSTROOT = "$(HOME)";
|
||||
INFOPLIST_FILE = SwiftLintXcode/Info.plist;
|
||||
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
|
||||
INFOPLIST_FILE = SwiftLintAutoCorrect/Info.plist;
|
||||
INSTALL_PATH = "/Library/Application Support/Developer/Shared/Xcode/Plug-ins";
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(DT_TOOLCHAIN_DIR)/usr/lib/swift/macosx";
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.11;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = net.ypresto.SwiftLintXcode;
|
||||
PRODUCT_NAME = SwiftLintXcode;
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "SwiftLintXcode/SwiftLintXcode-Bridging-Header.h";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = net.ypresto.SwiftLintAutoCorrect;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "SwiftLintAutoCorrect/SwiftLintAutoCorrect-Bridging-Header.h";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_VERSION = 3.0;
|
||||
WRAPPER_EXTENSION = xcplugin;
|
||||
};
|
||||
name = Debug;
|
||||
|
|
@ -262,19 +245,18 @@
|
|||
04E4BF3E1CB25D3200BC7305 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
COMBINE_HIDPI_IMAGES = YES;
|
||||
DEPLOYMENT_LOCATION = YES;
|
||||
DSTROOT = "$(HOME)";
|
||||
INFOPLIST_FILE = SwiftLintXcode/Info.plist;
|
||||
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
|
||||
INFOPLIST_FILE = SwiftLintAutoCorrect/Info.plist;
|
||||
INSTALL_PATH = "/Library/Application Support/Developer/Shared/Xcode/Plug-ins";
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(DT_TOOLCHAIN_DIR)/usr/lib/swift/macosx";
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.11;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = net.ypresto.SwiftLintXcode;
|
||||
PRODUCT_NAME = SwiftLintXcode;
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "SwiftLintXcode/SwiftLintXcode-Bridging-Header.h";
|
||||
SWIFT_VERSION = 3.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = net.ypresto.SwiftLintAutoCorrect;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "SwiftLintAutoCorrect/SwiftLintAutoCorrect-Bridging-Header.h";
|
||||
WRAPPER_EXTENSION = xcplugin;
|
||||
};
|
||||
name = Release;
|
||||
|
|
@ -282,7 +264,7 @@
|
|||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
04E4BF2B1CB25D3100BC7305 /* Build configuration list for PBXProject "SwiftLintXcode" */ = {
|
||||
04E4BF2B1CB25D3100BC7305 /* Build configuration list for PBXProject "SwiftLintAutoCorrect" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
04E4BF3A1CB25D3200BC7305 /* Debug */,
|
||||
|
|
@ -291,7 +273,7 @@
|
|||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
04E4BF3C1CB25D3200BC7305 /* Build configuration list for PBXNativeTarget "SwiftLintXcode" */ = {
|
||||
04E4BF3C1CB25D3200BC7305 /* Build configuration list for PBXNativeTarget "SwiftLintAutoCorrect" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
04E4BF3D1CB25D3200BC7305 /* Debug */,
|
||||
|
|
@ -2,6 +2,6 @@
|
|||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:/Users/yuya.tanaka/Dropbox/repo/SwiftLintXcode/SwiftLintXcode.xcodeproj">
|
||||
location = "self:SwiftLintAutoCorrect.xcodeproj">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0800"
|
||||
LastUpgradeVersion = "0500"
|
||||
version = "2.0">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "NO"
|
||||
|
|
@ -14,10 +14,10 @@
|
|||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "04E4BF2E1CB25D3100BC7305"
|
||||
BuildableName = "SwiftLintXcode.xcplugin"
|
||||
BlueprintName = "SwiftLintXcode"
|
||||
ReferencedContainer = "container:SwiftLintXcode.xcodeproj">
|
||||
BlueprintIdentifier = "890A4B3E171F031300AFE577"
|
||||
BuildableName = "SwiftLintAutoCorrect.xcplugin"
|
||||
BlueprintName = "SwiftLintAutoCorrect"
|
||||
ReferencedContainer = "container:SwiftLintAutoCorrect.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
|
|
@ -29,56 +29,52 @@
|
|||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "8966AFCA173EB7BF004150C0"
|
||||
BuildableName = "SwiftLintXcodeTests.xctest"
|
||||
BlueprintName = "SwiftLintXcodeTests"
|
||||
ReferencedContainer = "container:SwiftLintXcode.xcodeproj">
|
||||
BuildableName = "SwiftLintAutoCorrectTests.xctest"
|
||||
BlueprintName = "SwiftLintAutoCorrectTests"
|
||||
ReferencedContainer = "container:SwiftLintAutoCorrect.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
buildConfiguration = "Debug">
|
||||
<Testables>
|
||||
</Testables>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
buildConfiguration = "Debug"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "NO"
|
||||
debugXPCServices = "NO"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "NO"
|
||||
viewDebuggingEnabled = "No">
|
||||
<PathRunnable
|
||||
runnableDebuggingMode = "0"
|
||||
FilePath = "/Applications/Xcode.app">
|
||||
</PathRunnable>
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "04E4BF2E1CB25D3100BC7305"
|
||||
BuildableName = "SwiftLintXcode.xcplugin"
|
||||
BlueprintName = "SwiftLintXcode"
|
||||
ReferencedContainer = "container:SwiftLintXcode.xcodeproj">
|
||||
BlueprintIdentifier = "890A4B3E171F031300AFE577"
|
||||
BuildableName = "SwiftLintAutoCorrect.xcplugin"
|
||||
BlueprintName = "SwiftLintAutoCorrect"
|
||||
ReferencedContainer = "container:SwiftLintAutoCorrect.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
buildConfiguration = "Release"
|
||||
debugDocumentVersioning = "YES">
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>SuppressBuildableAutocreation</key>
|
||||
<dict>
|
||||
<key>04E4BF2E1CB25D3100BC7305</key>
|
||||
<dict>
|
||||
<key>primary</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
145
SwiftLintAutoCorrect/Formatter.swift
Normal file
145
SwiftLintAutoCorrect/Formatter.swift
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
//
|
||||
// Formatter.swift
|
||||
// SwiftLintAutoCorrect
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/04.
|
||||
// Copyright (c) 2016 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Cocoa
|
||||
|
||||
final class Formatter {
|
||||
static var sharedInstance = Formatter()
|
||||
private static let pathExtension = "swiftlintautocorrect"
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
let tempDirURL: NSURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("SwiftLintAutoCorrect-\(NSUUID().UUIDString)")
|
||||
|
||||
struct CursorPosition {
|
||||
let line: Int
|
||||
let column: Int
|
||||
}
|
||||
|
||||
|
||||
class func isFormattableDocument(document: NSDocument) -> Bool {
|
||||
return (document.fileURL?.pathExtension?.lowercaseString == "swift") ?? false
|
||||
}
|
||||
|
||||
func tryFormatDocument(document: IDESourceCodeDocument) -> Bool {
|
||||
do {
|
||||
try formatDocument(document)
|
||||
return true
|
||||
} catch let error as NSError {
|
||||
NSAlert(error: error).runModal()
|
||||
} catch {
|
||||
NSAlert(error: NSError(domain: "net.ypresto.swiftlintautocorrect", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: "Unknown error occured: \(error)"
|
||||
])).runModal()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func formatDocument(document: IDESourceCodeDocument) throws {
|
||||
let textStorage: DVTSourceTextStorage = document.textStorage()
|
||||
let originalString = textStorage.string
|
||||
let formattedString = try formatString(originalString)
|
||||
if formattedString == originalString { return }
|
||||
|
||||
let selectedRange = SwiftLintAutoCorrectTRVSXcode.textView().selectedRange()
|
||||
let cursorPosition = cursorPositionForSelectedRange(selectedRange, textStorage: textStorage)
|
||||
|
||||
textStorage.beginEditing()
|
||||
textStorage.replaceCharactersInRange(NSRange(location: 0, length: textStorage.length), withString: formattedString, withUndoManager: document.undoManager())
|
||||
textStorage.endEditing()
|
||||
|
||||
let newLocation = locationForCursorPosition(cursorPosition, textStorage: textStorage)
|
||||
SwiftLintAutoCorrectTRVSXcode.textView().setSelectedRange(NSRange(location: newLocation, length: 0))
|
||||
}
|
||||
|
||||
private func cursorPositionForSelectedRange(selectedRange: NSRange, textStorage: DVTSourceTextStorage) -> CursorPosition {
|
||||
let line = textStorage.lineRangeForCharacterRange(selectedRange).location
|
||||
let column = selectedRange.location - startLocationOfLine(line, textStorage: textStorage)
|
||||
return CursorPosition(line: line, column: column)
|
||||
}
|
||||
|
||||
private func locationForCursorPosition(cursorPosition: CursorPosition, textStorage: DVTSourceTextStorage) -> Int {
|
||||
let startOfLine = startLocationOfLine(cursorPosition.line, textStorage: textStorage)
|
||||
let locationOfNextLine = textStorage.characterRangeForLineRange(NSRange(location: cursorPosition.line + 1, length: 0)).location
|
||||
// XXX: Can reach EOF..? Cursor position may be trimmed one charactor when cursor is on EOF.
|
||||
return min(startOfLine + cursorPosition.column, locationOfNextLine - 1)
|
||||
}
|
||||
|
||||
private func startLocationOfLine(line: Int, textStorage: DVTSourceTextStorage) -> Int {
|
||||
return textStorage.characterRangeForLineRange(NSRange(location: line, length: 0)).location
|
||||
}
|
||||
|
||||
private func formatString(string: String) throws -> String {
|
||||
return try withTempporaryFile { (filePath) in
|
||||
try string.writeToFile(filePath, atomically: false, encoding: NSUTF8StringEncoding)
|
||||
let swiftlintPath = try self.getExecutableOnPath("swiftlint")
|
||||
let task = NSTask.launchedTaskWithLaunchPath(swiftlintPath, arguments: [
|
||||
"autocorrect", "--path", filePath
|
||||
])
|
||||
task.waitUntilExit()
|
||||
if task.terminationStatus != 0 {
|
||||
throw NSError(domain: "net.ypresto.swiftlintautocorrect", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: "Executing swiftlint exited with non-zero status."
|
||||
])
|
||||
}
|
||||
return try String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding)
|
||||
}
|
||||
}
|
||||
|
||||
private func getExecutableOnPath(name: String) throws -> String {
|
||||
let pipe = NSPipe()
|
||||
let task = NSTask()
|
||||
task.launchPath = "/bin/bash"
|
||||
task.arguments = [
|
||||
"-l", "-c", "which \(name)"
|
||||
]
|
||||
task.standardOutput = pipe
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
if task.terminationStatus != 0 {
|
||||
throw NSError(domain: "net.ypresto.swiftlintautocorrect", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: "Executing `which swiftlint` exited with non-zero status."
|
||||
])
|
||||
}
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
guard let pathString = String(data: data, encoding: NSUTF8StringEncoding) else {
|
||||
throw NSError(domain: "net.ypresto.swiftlintautocorrect", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: "Cannot read result of `which swiftlint`."
|
||||
])
|
||||
}
|
||||
let path = pathString.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
|
||||
if !fileManager.isExecutableFileAtPath(path) {
|
||||
throw NSError(domain: "net.ypresto.swiftlintautocorrect", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: "swiftlint at \(path) is not executable."
|
||||
])
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
private func withTempporaryFile<T>(@noescape callback: (filePath: String) throws -> T) throws -> T {
|
||||
try ensureTemporaryDirectory()
|
||||
let filePath = createTemporaryPath()
|
||||
if fileManager.fileExistsAtPath(filePath) {
|
||||
throw NSError(domain: "net.ypresto.swiftlintautocorrect", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: "Cannot write to \(filePath), file already exists."
|
||||
])
|
||||
}
|
||||
defer { _ = try? fileManager.removeItemAtPath(filePath) }
|
||||
return try callback(filePath: filePath)
|
||||
}
|
||||
|
||||
private func createTemporaryPath() -> String {
|
||||
return tempDirURL.URLByAppendingPathComponent(NSUUID().UUIDString).path! + ".swift"
|
||||
}
|
||||
|
||||
private func ensureTemporaryDirectory() throws {
|
||||
if fileManager.fileExistsAtPath(tempDirURL.path!) { return }
|
||||
try fileManager.createDirectoryAtURL(tempDirURL, withIntermediateDirectories: true, attributes: nil)
|
||||
}
|
||||
}
|
||||
|
|
@ -38,16 +38,11 @@
|
|||
<string>0420B86A-AA43-4792-9ED0-6FE0F2B16A13</string>
|
||||
<string>7265231C-39B4-402C-89E1-16167C4CC990</string>
|
||||
<string>ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C</string>
|
||||
<string>8A66E736-A720-4B3C-92F1-33D9962C69DF</string>
|
||||
<string>65C57D32-1E9B-44B8-8C04-A27BA7AAE2C4</string>
|
||||
<string>DA4FDFD8-C509-4D8B-8B55-84A7B66AE701</string>
|
||||
<string>E0A62D1F-3C18-4D74-BFE5-A4167D643966</string>
|
||||
<string>DFFB3951-EB0A-4C09-9DAC-5F2D28CC839C</string>
|
||||
</array>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
<string>SwiftLintXcode</string>
|
||||
<string>SwiftLintAutoCorrect</string>
|
||||
<key>XC4Compatible</key>
|
||||
<true/>
|
||||
<key>XCPluginHasUI</key>
|
||||
|
|
@ -8,12 +8,12 @@
|
|||
import Foundation
|
||||
|
||||
extension NSObject {
|
||||
class func pluginDidLoad(_ bundle: Bundle) {
|
||||
let appName = Bundle.main.infoDictionary?["CFBundleName"] as? NSString
|
||||
class func pluginDidLoad(bundle: NSBundle) {
|
||||
let appName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? NSString
|
||||
if appName == "Xcode" {
|
||||
if sharedPlugin == nil {
|
||||
sharedPlugin = SwiftLintXcode(bundle: bundle)
|
||||
sharedPlugin = SwiftLintAutoCorrect(bundle: bundle)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
//
|
||||
// SaveHook.swift
|
||||
// SwiftLintXcode
|
||||
// SwiftLintAutoCorrect
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/04.
|
||||
// Copyright (c) 2016 Yuya Tanaka. All rights reserved.
|
||||
|
|
@ -24,15 +24,15 @@ final class SaveHook {
|
|||
if swizzled { return }
|
||||
swizzled = true
|
||||
|
||||
let fromMethod = class_getInstanceMethod(NSDocument.self, #selector(NSDocument.save(withDelegate:didSave:contextInfo:)))
|
||||
let toMethod = class_getInstanceMethod(NSDocument.self, #selector(NSDocument.swiftLintXcodeSaveDocument(delegate:didSaveSelector:contextInfo:)))
|
||||
let fromMethod = class_getInstanceMethod(NSDocument.self, #selector(NSDocument.saveDocumentWithDelegate(_:didSaveSelector:contextInfo:)))
|
||||
let toMethod = class_getInstanceMethod(NSDocument.self, #selector(NSDocument.swiftLintAutoCorrectSaveDocumentWithDelegate(_:didSaveSelector:contextInfo:)))
|
||||
method_exchangeImplementations(fromMethod, toMethod)
|
||||
}
|
||||
|
||||
class func tryOnSaveDocument(_ document: NSDocument) -> Bool {
|
||||
class func tryOnSaveDocument(document: NSDocument) -> Bool {
|
||||
if !enabled { return true }
|
||||
if !Formatter.isFormattableDocument(document) { return true }
|
||||
let sourceCodeDocument: IDESourceCodeDocument = SwiftLintXcodeTRVSXcode.sourceCodeDocument()
|
||||
Formatter.isFormattableDocument(document)
|
||||
let sourceCodeDocument: IDESourceCodeDocument = SwiftLintAutoCorrectTRVSXcode.sourceCodeDocument()
|
||||
guard sourceCodeDocument == document else { return true }
|
||||
return Formatter.sharedInstance.tryFormatDocument(sourceCodeDocument)
|
||||
}
|
||||
|
|
@ -41,10 +41,10 @@ final class SaveHook {
|
|||
// https://github.com/travisjeffery/ClangFormat-Xcode/blob/a22114907592fb5d5b1043a4919d7be3e1496741/ClangFormat/NSDocument+TRVSClangFormat.m
|
||||
extension NSDocument {
|
||||
|
||||
dynamic func swiftLintXcodeSaveDocument(delegate: AnyObject?, didSaveSelector: Selector, contextInfo: UnsafeMutableRawPointer) -> Void {
|
||||
dynamic func swiftLintAutoCorrectSaveDocumentWithDelegate(delegate: AnyObject?, didSaveSelector: Selector, contextInfo: UnsafeMutablePointer<Void>) -> Void {
|
||||
if SaveHook.tryOnSaveDocument(self) {
|
||||
// NOTE: Call original method
|
||||
swiftLintXcodeSaveDocument(delegate: delegate, didSaveSelector: didSaveSelector, contextInfo: contextInfo)
|
||||
swiftLintAutoCorrectSaveDocumentWithDelegate(delegate, didSaveSelector: didSaveSelector, contextInfo: contextInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,5 +2,4 @@
|
|||
// Use this file to import your target's public headers that you would like to expose to Swift.
|
||||
//
|
||||
|
||||
#import "SwiftLintXcodeTRVSXcode.h"
|
||||
#import "SwiftLintXcodeIDEHelper.h"
|
||||
#import "SwiftLintAutoCorrectTRVSXcode.h"
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// SwiftLintXcode.swift
|
||||
// SwiftLintAutoCorrect.swift
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/04.
|
||||
// Copyright (c) 2016 Yuya Tanaka. All rights reserved.
|
||||
|
|
@ -7,21 +7,21 @@
|
|||
|
||||
import AppKit
|
||||
|
||||
var sharedPlugin: SwiftLintXcode?
|
||||
var sharedPlugin: SwiftLintAutoCorrect?
|
||||
|
||||
class SwiftLintXcode: NSObject {
|
||||
class SwiftLintAutoCorrect: NSObject {
|
||||
|
||||
var bundle: Bundle
|
||||
lazy var center = NotificationCenter.default
|
||||
var bundle: NSBundle
|
||||
lazy var center = NSNotificationCenter.defaultCenter()
|
||||
|
||||
var enableMenuItem: NSMenuItem!
|
||||
var disableMenuItem: NSMenuItem!
|
||||
|
||||
init(bundle: Bundle) {
|
||||
init(bundle: NSBundle) {
|
||||
self.bundle = bundle
|
||||
|
||||
super.init()
|
||||
center.addObserver(self, selector: #selector(SwiftLintXcode.onApplicationDidFinishLaunching), name: NSNotification.Name.NSApplicationDidFinishLaunching, object: nil)
|
||||
center.addObserver(self, selector: #selector(SwiftLintAutoCorrect.onApplicationDidFinishLaunching), name: NSApplicationDidFinishLaunchingNotification, object: nil)
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
|
@ -40,34 +40,34 @@ class SwiftLintXcode: NSObject {
|
|||
private func createMenuItems() {
|
||||
removeObserver()
|
||||
|
||||
guard let item = NSApp.mainMenu!.item(withTitle: "Edit") else { return }
|
||||
guard let item = NSApp.mainMenu!.itemWithTitle("Edit") else { return }
|
||||
|
||||
let pluginMenu = NSMenu(title:"SwiftLintXcode")
|
||||
let pluginMenuItem = NSMenuItem(title:"SwiftLintXcode", action: nil, keyEquivalent: "")
|
||||
let pluginMenu = NSMenu(title:"SwiftLint Auto Correct")
|
||||
let pluginMenuItem = NSMenuItem(title:"SwiftLint Auto Correct", action: nil, keyEquivalent: "")
|
||||
pluginMenuItem.submenu = pluginMenu
|
||||
|
||||
let autoCorrectMenuItem = NSMenuItem(title:"AutoCorrect Current File", action:#selector(SwiftLintXcode.doAutoCorrect), keyEquivalent:"")
|
||||
let autoCorrectMenuItem = NSMenuItem(title:"Auto Correct", action:#selector(SwiftLintAutoCorrect.doAutoCorrect), keyEquivalent:"")
|
||||
autoCorrectMenuItem.target = self
|
||||
pluginMenu.addItem(autoCorrectMenuItem)
|
||||
|
||||
let enableMenuItem = NSMenuItem(title:"Enable AutoCorrect on Save", action:#selector(SwiftLintXcode.doEnableFormatOnSave), keyEquivalent:"")
|
||||
let enableMenuItem = NSMenuItem(title:"Enable Format on Save", action:#selector(SwiftLintAutoCorrect.doEnableFormatOnSave), keyEquivalent:"")
|
||||
enableMenuItem.target = self
|
||||
pluginMenu.addItem(enableMenuItem)
|
||||
self.enableMenuItem = enableMenuItem
|
||||
|
||||
let disableMenuItem = NSMenuItem(title:"Disable AutoCorrect on Save", action:#selector(SwiftLintXcode.doDisableFormatOnSave), keyEquivalent:"")
|
||||
let disableMenuItem = NSMenuItem(title:"Disable Format on Save", action:#selector(SwiftLintAutoCorrect.doDisableFormatOnSave), keyEquivalent:"")
|
||||
disableMenuItem.target = self
|
||||
pluginMenu.addItem(disableMenuItem)
|
||||
self.disableMenuItem = disableMenuItem
|
||||
|
||||
item.submenu!.addItem(NSMenuItem.separator())
|
||||
item.submenu!.addItem(NSMenuItem.separatorItem())
|
||||
item.submenu!.addItem(pluginMenuItem)
|
||||
|
||||
updateMenuVisibility()
|
||||
}
|
||||
|
||||
func doAutoCorrect() {
|
||||
let sourceCodeDocument: IDESourceCodeDocument = SwiftLintXcodeTRVSXcode.sourceCodeDocument()
|
||||
let sourceCodeDocument: IDESourceCodeDocument = SwiftLintAutoCorrectTRVSXcode.sourceCodeDocument()
|
||||
guard Formatter.isFormattableDocument(sourceCodeDocument) else { return }
|
||||
Formatter.sharedInstance.tryFormatDocument(sourceCodeDocument)
|
||||
}
|
||||
|
|
@ -83,7 +83,7 @@ class SwiftLintXcode: NSObject {
|
|||
}
|
||||
|
||||
func updateMenuVisibility() {
|
||||
self.enableMenuItem.isHidden = SaveHook.enabled
|
||||
self.disableMenuItem.isHidden = !SaveHook.enabled
|
||||
self.enableMenuItem.hidden = SaveHook.enabled
|
||||
self.disableMenuItem.hidden = !SaveHook.enabled
|
||||
}
|
||||
}
|
||||
|
|
@ -123,7 +123,7 @@
|
|||
@property (readonly) IDEWorkspace *workspace;
|
||||
@end
|
||||
|
||||
@interface SwiftLintXcodeTRVSXcode : NSObject
|
||||
@interface SwiftLintAutoCorrectTRVSXcode : NSObject
|
||||
|
||||
+ (IDESourceCodeDocument *)sourceCodeDocument;
|
||||
+ (NSTextView *)textView;
|
||||
|
|
@ -6,9 +6,9 @@
|
|||
// Copyright (c) 2014 Travis Jeffery. All rights reserved.
|
||||
//
|
||||
|
||||
#import "SwiftLintXcodeTRVSXcode.h"
|
||||
#import "SwiftLintAutoCorrectTRVSXcode.h"
|
||||
|
||||
@implementation SwiftLintXcodeTRVSXcode
|
||||
@implementation SwiftLintAutoCorrectTRVSXcode
|
||||
|
||||
+ (id)currentEditor {
|
||||
if ([[self windowController]
|
||||
|
|
@ -1,133 +0,0 @@
|
|||
//
|
||||
// Formatter.swift
|
||||
// SwiftLintXcode
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/04.
|
||||
// Copyright (c) 2016 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Cocoa
|
||||
|
||||
final class Formatter {
|
||||
static var sharedInstance = Formatter()
|
||||
|
||||
private static let pathExtension = "SwiftLintXcode"
|
||||
private let fileManager = FileManager.default
|
||||
|
||||
private struct CursorPosition {
|
||||
let line: Int
|
||||
let column: Int
|
||||
}
|
||||
|
||||
class func isFormattableDocument(_ document: NSDocument) -> Bool {
|
||||
return document.fileURL?.pathExtension.lowercased() == "swift"
|
||||
}
|
||||
|
||||
func tryFormatDocument(_ document: IDESourceCodeDocument) -> Bool {
|
||||
do {
|
||||
try formatDocument(document)
|
||||
return true
|
||||
} catch let error as NSError {
|
||||
NSAlert(error: error).runModal()
|
||||
} catch {
|
||||
NSAlert(error: errorWithMessage("Unknown error occured: \(error)")).runModal()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func formatDocument(_ document: IDESourceCodeDocument) throws {
|
||||
let textStorage: DVTSourceTextStorage = document.textStorage()
|
||||
let originalString = textStorage.string
|
||||
let formattedString = try formatString(originalString)
|
||||
if formattedString == originalString { return }
|
||||
|
||||
let selectedRange = SwiftLintXcodeTRVSXcode.textView().selectedRange()
|
||||
let cursorPosition = cursorPositionForSelectedRange(selectedRange, textStorage: textStorage)
|
||||
|
||||
textStorage.beginEditing()
|
||||
textStorage.replaceCharacters(in: NSRange(location: 0, length: textStorage.length), with: formattedString, withUndoManager: document.undoManager())
|
||||
textStorage.endEditing()
|
||||
|
||||
let newLocation = locationForCursorPosition(cursorPosition, textStorage: textStorage)
|
||||
SwiftLintXcodeTRVSXcode.textView().setSelectedRange(NSRange(location: newLocation, length: 0))
|
||||
}
|
||||
|
||||
private func cursorPositionForSelectedRange(_ selectedRange: NSRange, textStorage: DVTSourceTextStorage) -> CursorPosition {
|
||||
let line = textStorage.lineRange(forCharacterRange: selectedRange).location
|
||||
let column = selectedRange.location - startLocationOfLine(line, textStorage: textStorage)
|
||||
return CursorPosition(line: line, column: column)
|
||||
}
|
||||
|
||||
private func locationForCursorPosition(_ cursorPosition: CursorPosition, textStorage: DVTSourceTextStorage) -> Int {
|
||||
let startOfLine = startLocationOfLine(cursorPosition.line, textStorage: textStorage)
|
||||
let locationOfNextLine = textStorage.characterRange(forLineRange: NSRange(location: cursorPosition.line + 1, length: 0)).location
|
||||
// XXX: Can reach EOF..? Cursor position may be trimmed one charactor when cursor is on EOF.
|
||||
return min(startOfLine + cursorPosition.column, locationOfNextLine - 1)
|
||||
}
|
||||
|
||||
private func startLocationOfLine(_ line: Int, textStorage: DVTSourceTextStorage) -> Int {
|
||||
return textStorage.characterRange(forLineRange: NSRange(location: line, length: 0)).location
|
||||
}
|
||||
|
||||
private func formatString(_ string: String) throws -> String {
|
||||
guard let workspaceRootDirectory = SwiftLintXcodeIDEHelper.currentWorkspaceURL()?.deletingLastPathComponent().path else {
|
||||
throw errorWithMessage("Cannot determine project directory.")
|
||||
}
|
||||
|
||||
return try withTempporaryFile { (filePath) in
|
||||
try string.write(toFile: filePath, atomically: false, encoding: String.Encoding.utf8)
|
||||
let swiftlintPath = try self.getExecutableOnPath(name: "swiftlint", workingDirectory: workspaceRootDirectory)
|
||||
let task = Process()
|
||||
task.launchPath = swiftlintPath
|
||||
task.arguments = ["autocorrect", "--path", filePath]
|
||||
task.currentDirectoryPath = workspaceRootDirectory
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
if task.terminationStatus != 0 {
|
||||
throw errorWithMessage("Executing swiftlint exited with non-zero status.")
|
||||
}
|
||||
return try String(contentsOfFile: filePath, encoding: String.Encoding.utf8)
|
||||
}
|
||||
}
|
||||
|
||||
private func getExecutableOnPath(name: String, workingDirectory: String) throws -> String {
|
||||
let pipe = Pipe()
|
||||
let task = Process()
|
||||
task.launchPath = "/bin/bash"
|
||||
task.arguments = [
|
||||
"-l", "-c", "which \(name)"
|
||||
]
|
||||
task.currentDirectoryPath = workingDirectory
|
||||
task.standardOutput = pipe
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
if task.terminationStatus != 0 {
|
||||
throw errorWithMessage("Executing `which swiftlint` exited with non-zero status.")
|
||||
}
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
guard let pathString = String(data: data, encoding: String.Encoding.utf8) else {
|
||||
throw errorWithMessage("Cannot read result of `which swiftlint`.")
|
||||
}
|
||||
let path = pathString.trimmingCharacters(in: CharacterSet.newlines)
|
||||
if !fileManager.isExecutableFile(atPath: path) {
|
||||
throw errorWithMessage("swiftlint at \(path) is not executable.")
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
private func withTempporaryFile<T>(_ callback: (_ filePath: String) throws -> T) throws -> T {
|
||||
let filePath = createTemporaryPath()
|
||||
if fileManager.fileExists(atPath: filePath) {
|
||||
throw errorWithMessage("Cannot write to \(filePath), file already exists.")
|
||||
}
|
||||
defer { _ = try? fileManager.removeItem(atPath: filePath) }
|
||||
return try callback(filePath)
|
||||
}
|
||||
|
||||
private func createTemporaryPath() -> String {
|
||||
return URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
.appendingPathComponent("SwiftLintXcode_\(UUID().uuidString).swift").path
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
//
|
||||
// SwiftLintXcodeIDEHelper.h
|
||||
// SwiftLintXcode
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/06.
|
||||
// Copyright © 2016年 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface SwiftLintXcodeIDEHelper : NSObject
|
||||
|
||||
+ (nullable NSURL *)currentWorkspaceURL;
|
||||
|
||||
@end
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
//
|
||||
// SwiftLintXcodeIDEHelper.m
|
||||
// SwiftLintXcode
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/06.
|
||||
// Copyright © 2016年 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
#import "SwiftLintXcodeIDEHelper.h"
|
||||
#import "SwiftLintXcodeTRVSXcode.h"
|
||||
@import Cocoa;
|
||||
|
||||
@implementation SwiftLintXcodeIDEHelper
|
||||
|
||||
+ (nullable NSURL *)currentWorkspaceURL
|
||||
{
|
||||
IDEWorkspaceWindowController *workspaceWindowController = (IDEWorkspaceWindowController *)[[NSApp keyWindow] windowController];
|
||||
IDEWorkspace *workspace = [workspaceWindowController valueForKey:@"_workspace"];
|
||||
return workspace.representingFilePath.fileURL;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
//
|
||||
// errorHelper.swift
|
||||
// SwiftLintXcode
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/06.
|
||||
// Copyright © 2016年 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
func errorWithMessage(_ message: String) -> NSError {
|
||||
return NSError(domain: "net.ypresto.SwiftLintXcode", code: 0, userInfo: [
|
||||
NSLocalizedDescriptionKey: message
|
||||
])
|
||||
}
|
||||
Loading…
Reference in a new issue