Skip to content

Commit

Permalink
Merge branch 'release/0.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
intitni committed Aug 21, 2023
2 parents 48dc17c + ec02df5 commit 8cb53c8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
33 changes: 25 additions & 8 deletions EditorExtensionXPCService/Formatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct ProjectConfig: Codable {
var clangFormatStyle: String?
var usePrettierFromNodeModules: Bool?
var prettierExecutablePath: String?
var prettierArguments: String?
}

protocol Formatter {
Expand Down Expand Up @@ -126,6 +127,8 @@ struct ClangFormat: Formatter {
struct Prettier: Formatter {
var executablePath: String? { Settings.defaultPrettierExecutablePath }

var arguments: String? { Settings.defaultPrettierArguments }

var configurationFileName: NSRegularExpression {
try! .init(pattern: #"^\.prettierrc(\..*)?$"#, options: [.caseInsensitive])
}
Expand Down Expand Up @@ -154,6 +157,7 @@ struct Prettier: Formatter {
confURL _: URL?,
projectConfig: ProjectConfig?
) async throws {
let arguments = projectConfig?.prettierArguments ?? arguments ?? ""
if let usePrettierFromNodeModules = projectConfig?.usePrettierFromNodeModules,
usePrettierFromNodeModules
{
Expand All @@ -162,16 +166,24 @@ struct Prettier: Formatter {
try await runCommand(
from: "/usr/bin/env",
currentDirectoryURL: currentDirectoryURL,
args: executablePath, "prettier", "--write", file.path
args: executablePath, "prettier", file.path, "--write", arguments
)
} else {
guard let executablePath = projectConfig?.prettierExecutablePath ?? executablePath
else { throw ExtensionError.other("Prettier executable path not set.") }
try await runCommand(
from: "/usr/bin/env",
currentDirectoryURL: currentDirectoryURL,
args: executablePath, "--write", file.path
)
if arguments.isEmpty {
try await runCommand(
from: "/usr/bin/env",
currentDirectoryURL: currentDirectoryURL,
args: executablePath, "--write", file.path
)
} else {
try await runCommand(
from: ProcessInfo.processInfo.environment["SHELL"] ?? "/bin/bash",
currentDirectoryURL: currentDirectoryURL,
args: "-ilc", "\(executablePath) \(file.path) --write \(arguments)"
)
}
}
}

Expand Down Expand Up @@ -203,17 +215,21 @@ private func runCommand(
]
let outpipe = Pipe()
task.standardOutput = outpipe
task.standardError = outpipe
task.terminationHandler = { task in
if let data = try? outpipe.fileHandleForReading.readToEnd(),
let text = String(data: data, encoding: .utf8)
{
if task.terminationStatus == 0 {
continuation.resume(returning: text)
} else {
continuation.resume(throwing: ExtensionError.other(text))
continuation.resume(
throwing: ExtensionError
.other(text.trimmingCharacters(in: .whitespacesAndNewlines))
)
}
} else {
continuation.resume(returning: "")
continuation.resume(throwing: ExtensionError.other("Unknown error"))
}
}
try task.run()
Expand All @@ -222,3 +238,4 @@ private func runCommand(
}
}
}

12 changes: 10 additions & 2 deletions EditorExtensionXPCService/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ final class Service {

formatter = formatters.first {
$0.hasValidExecutablePath(projectConfiguration: projectConfig)
}
} ?? Prettier()
return (formatter, confURL, projectConfig, fileDirectory)
}

Expand All @@ -161,7 +161,15 @@ final class Service {
in projectURL: URL?
) async throws -> String {
let data = content.data(using: .utf8)
let tempDirectory = projectURL ?? FileManager.default.temporaryDirectory
let tempDirectory = {
if let projectURL {
if projectURL.pathExtension == "playground" {
return FileManager.default.temporaryDirectory
}
return projectURL
}
return FileManager.default.temporaryDirectory
}()
let fileName = ".xccurate_formatter_\(UUID().uuidString).\(fileExtension ?? "")"
let fileURL: URL
if #available(macOS 13.0, *) {
Expand Down
5 changes: 5 additions & 0 deletions EditorExtensionXPCService/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ enum Settings {
string(forKey: SettingsKey.defaultPrettierExecutablePath, from: storage)
}

static var defaultPrettierArguments: String? {
string(forKey: SettingsKey.defaultPrettierArguments, from: storage)
}

static var defaultNPXExecutablePath: String? {
string(forKey: SettingsKey.defaultNPXExecutablePath, from: storage)
}
Expand All @@ -42,3 +46,4 @@ private func string(forKey key: String, from userDefaults: UserDefaults) -> Stri
private func bool(forKey key: String, from userDefaults: UserDefaults) -> Bool {
userDefaults.bool(forKey: key)
}

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ Place the formatter configuration files at the project root or its parent direct

If no configuration is found, it will use the first formatter in the supported formatters list that supports the language and has its executable path set.

### Prettier Plugins

It's possible to use Prettier plugins. To add a plugin, you should setup the Prettier Arguments in the settings.

For example, if you want to use the Ruby plugin, you will have to set the text field to

```
--plugin=/opt/homebrew/lib/node_modules/@prettier/plugin-ruby/src/plugin.js
```

When the arguments field in not empty, the app will run Prettier in an interactive logged-in (`-ilc`) shell so that it can find other dependencies installed by the plugin.

### Key Bindings

You can set key bindings for the command in Xcode settings.
Expand Down
8 changes: 4 additions & 4 deletions XccurateFormatter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 40;
CURRENT_PROJECT_VERSION = 50;
DEVELOPMENT_ASSET_PATHS = "\"XccurateFormatter/Preview Content\"";
DEVELOPMENT_TEAM = 5YKZ4Y3DAW;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -778,7 +778,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 0.6.0;
MARKETING_VERSION = 0.7.0;
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -796,7 +796,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 40;
CURRENT_PROJECT_VERSION = 50;
DEVELOPMENT_ASSET_PATHS = "\"XccurateFormatter/Preview Content\"";
DEVELOPMENT_TEAM = 5YKZ4Y3DAW;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -810,7 +810,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 0.6.0;
MARKETING_VERSION = 0.7.0;
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
13 changes: 12 additions & 1 deletion XccurateFormatter/PrettierSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ struct PrettierSettings: View {
var defaultPrettierExecutablePath: String = ""
@AppStorage(SettingsKey.defaultNPXExecutablePath, store: .shared)
var defaultNPXExecutablePath: String = ""
@AppStorage(SettingsKey.defaultPrettierArguments, store: .shared)
var defaultPrettierArguments: String = ""

var body: some View {
Card(
Expand All @@ -20,6 +22,14 @@ struct PrettierSettings: View {
.textFieldStyle(.roundedBorder)
.autocorrectionDisabled(true)

TextField(
"Prettier Arguments",
text: $defaultPrettierArguments,
prompt: Text("arguments, e.g. --plugin=prettier-plugin-foo")
)
.textFieldStyle(.roundedBorder)
.autocorrectionDisabled(true)

TextField(
"NPX Executable Path",
text: $defaultNPXExecutablePath,
Expand All @@ -45,7 +55,7 @@ struct PrettierSettings: View {
}
Cheatsheet {
Text(
"support languages: `JavaScript, tsx, CSS, Less, SCSS, HTML, JSON, Markdown, YAML, XML`"
"support languages: `JavaScript, tsx, CSS, Less, SCSS, HTML, JSON, Markdown, YAML, XML, and others with plugins."
)
}
}
Expand All @@ -58,3 +68,4 @@ struct PrettierSettings_Previews: PreviewProvider {
PrettierSettings()
}
}

1 change: 1 addition & 0 deletions XccurateFormatter/UserDefaultsKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ enum SettingsKey {
static let defaultClangFormatExecutablePath = "defaultClangFormatExecutablePath"
static let defaultClangFormatStyle = "defaultClangFormatStyle"
static let defaultPrettierExecutablePath = "defaultPrettierExecutablePath"
static let defaultPrettierArguments = "defaultPrettierArguments"
static let defaultNPXExecutablePath = "defaultNPXExecutablePath"
}

0 comments on commit 8cb53c8

Please sign in to comment.