Skip to content

Commit

Permalink
Move URLUtils to EditorCore
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 17, 2024
1 parent 080f530 commit f6d31a9
Show file tree
Hide file tree
Showing 26 changed files with 56 additions and 51 deletions.
26 changes: 0 additions & 26 deletions CotEditor.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CotEditor/Sources/AppleScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import Foundation
import Shortcut
import URLUtils

struct AppleScript: EventScript {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/CommandLineToolManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//

import Foundation
import URLUtils

final class CommandLineToolManager: Sendable {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/DirectoryDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import AppKit
import Observation
import UniformTypeIdentifiers
import OSLog
import URLUtils

@Observable final class DirectoryDocument: NSDocument {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import FileEncoding
import FilePermissions
import LineEnding
import Syntax
import URLUtils

extension Document: EditorSource {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/DocumentWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Combine
import SwiftUI
import Defaults
import ControlUI
import URLUtils

final class DocumentWindowController: NSWindowController, NSWindowDelegate {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/FileBrowserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import AppKit
import UniformTypeIdentifiers
import AudioToolbox
import Defaults
import URLUtils

struct FileBrowserView: View {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/KeyBindingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import AppKit
import Shortcut
import URLUtils

@MainActor final class KeyBindingManager {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/MultipleReplaceListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import UniformTypeIdentifiers
import OSLog
import Defaults
import TextFind
import URLUtils

final class MultipleReplaceListViewController: NSViewController, NSMenuItemValidation {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/PrintTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import AppKit
import Defaults
import URLUtils

final class PrintTextView: NSTextView {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/ReplacementManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Foundation
import Observation
import TextFind
import UniformTypeIdentifiers
import URLUtils

@Observable final class ReplacementManager: SettingFileManaging {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/ScriptManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import AppKit
import Combine
import Shortcut
import URLUtils

// NSObject-based NSAppleEventDescriptor must be used but not sendable
// -> According to the documentation, NSAppleEventDescriptor is just a wrapper of AEDesc,
Expand Down
16 changes: 16 additions & 0 deletions CotEditor/Sources/SettingFileManaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import Foundation
import UniformTypeIdentifiers
import AppKit.NSApplication
import URLUtils

enum SettingChange {

Expand Down Expand Up @@ -74,6 +75,21 @@ extension NSNotification.Name {
}


extension URL {

/// Returns the URL for the given subdirectory in the application support directory in the user domain.
///
/// - Parameter subDirectory: The name of the subdirectory in the application support.
/// - Returns: A directory URL.
static func applicationSupportDirectory(component subDirectory: String) -> URL {

.applicationSupportDirectory
.appending(component: "CotEditor", directoryHint: .isDirectory)
.appending(component: subDirectory, directoryHint: .isDirectory)
}
}



// MARK: -

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/SnippetManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Foundation
import AppKit.NSMenuItem
import Defaults
import Shortcut
import URLUtils

@MainActor @objc protocol SnippetInsertable: AnyObject {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/SyntaxListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import SwiftUI
import Syntax
import UniformTypeIdentifiers
import Defaults
import URLUtils

final class SyntaxListViewController: NSViewController, NSMenuItemValidation, NSTableViewDelegate, NSTableViewDataSource, NSFilePromiseProviderDelegate {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/SyntaxManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Yams
import Defaults
import Syntax
import SyntaxMap
import URLUtils

enum SyntaxName {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/ThemeListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import AudioToolbox
import Combine
import SwiftUI
import UniformTypeIdentifiers
import URLUtils

final class ThemeListViewController: NSViewController, NSMenuItemValidation, NSTableViewDelegate, NSTableViewDataSource, NSFilePromiseProviderDelegate, NSTextFieldDelegate {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/ThemeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Combine
import Foundation
import UniformTypeIdentifiers
import Defaults
import URLUtils

final class ThemeManager: SettingFileManaging {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/UTType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import UniformTypeIdentifiers

extension UTType {

/// All filename extensions.
var filenameExtensions: [String] {

self.tags[.filenameExtension] ?? []
Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/UnixScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import Foundation
import AppKit.NSDocument
import Shortcut
import URLUtils

struct UnixScript: Script {

Expand Down
5 changes: 5 additions & 0 deletions Packages/EditorCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let package = Package(
"EditedRangeSet",
"FileEncoding",
"FilePermissions",
"URLUtils",
"FuzzyRange",
"Invisible",
"LineEnding",
Expand All @@ -34,6 +35,7 @@ let package = Package(
.library(name: "EditedRangeSet", targets: ["EditedRangeSet"]),
.library(name: "FileEncoding", targets: ["FileEncoding"]),
.library(name: "FilePermissions", targets: ["FilePermissions"]),
.library(name: "URLUtils", targets: ["URLUtils"]),
.library(name: "FuzzyRange", targets: ["FuzzyRange"]),
.library(name: "Invisible", targets: ["Invisible"]),
.library(name: "LineEnding", targets: ["LineEnding"]),
Expand Down Expand Up @@ -64,6 +66,9 @@ let package = Package(
.target(name: "FilePermissions"),
.testTarget(name: "FilePermissionsTests", dependencies: ["FilePermissions"]),

.target(name: "URLUtils"),
.testTarget(name: "URLUtilsTests", dependencies: ["URLUtils"]),

.target(name: "FuzzyRange"),
.testTarget(name: "FuzzyRangeTests", dependencies: ["FuzzyRange"]),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// String+Filename.swift
// URLUtils
//
// CotEditor
// https://coteditor.com
Expand All @@ -25,7 +26,7 @@

import Foundation

extension String {
public extension String {

/// The remainder of string after last dot removed.
var deletingPathExtension: String {
Expand All @@ -35,7 +36,7 @@ extension String {
}


extension Collection<String> {
public extension Collection<String> {

/// Creates a unique name from the receiver's elements by adding the suffix and also a number if needed.
///
Expand All @@ -50,13 +51,14 @@ extension Collection<String> {
let (rootName, baseCount): (String, Int?) = {
let suffixPattern = NSRegularExpression.escapedPattern(for: spaceSuffix)
let regex = try! NSRegularExpression(pattern: suffixPattern + "(?: ([0-9]+))?$")
let range = NSRange(location: 0, length: proposedName.utf16.count)

guard let result = regex.firstMatch(in: proposedName, range: proposedName.nsRange) else { return (proposedName, nil) }
guard let result = regex.firstMatch(in: proposedName, range: range) else { return (proposedName, nil) }

let root = (proposedName as NSString).substring(to: result.range.location)
let numberRange = result.range(at: 1)

guard !numberRange.isNotFound else { return (root, nil) }
guard numberRange.location != NSNotFound else { return (root, nil) }

let number = Int((proposedName as NSString).substring(with: numberRange))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// URL+ExtendedAttribute.swift
// URLUtils
//
// CotEditor
// https://coteditor.com
Expand All @@ -25,7 +26,7 @@

import Foundation

extension URL {
public extension URL {

/// Gets extended attribute.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// URL.swift
// URLUtils
//
// CotEditor
// https://coteditor.com
Expand All @@ -25,7 +26,7 @@

import Foundation

extension URL {
public extension URL {

/// Simply checks the reachability of the URL by ignoring errors.
var isReachable: Bool {
Expand Down Expand Up @@ -109,7 +110,7 @@ extension URL {

// MARK: User Domain

extension URL {
public extension URL {

/// A temporary URL in the user domain for file replacement.
static var itemReplacementDirectory: URL {
Expand All @@ -118,22 +119,10 @@ extension URL {
try URL(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: .userDirectory, create: true)
}
}


/// Returns the URL for the given subdirectory in the application support directory in the user domain.
///
/// - Parameter subDirectory: The name of the subdirectory in the application support.
/// - Returns: A directory URL.
static func applicationSupportDirectory(component subDirectory: String) -> URL {

.applicationSupportDirectory
.appending(component: "CotEditor", directoryHint: .isDirectory)
.appending(component: subDirectory, directoryHint: .isDirectory)
}
}


extension FileManager {
public extension FileManager {

/// Creates intermediate directories to the given URL if not available.
///
Expand All @@ -147,7 +136,7 @@ extension FileManager {

// MARK: Sandboxing

extension URL {
public extension URL {

private static let homeDirectory = getpwuid(getuid())?.pointee.pw_dir.flatMap { String(cString: $0) } ?? NSHomeDirectory()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// StringFilename.swift
// Tests
// URLUtilsTests
//
// CotEditor
// https://coteditor.com
Expand All @@ -25,7 +25,7 @@
//

import Testing
@testable import CotEditor
@testable import URLUtils

struct StringFilename {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// URLExtensionsTests.swift
// Tests
// URLUtilsTests
//
// CotEditor
// https://coteditor.com
Expand All @@ -26,7 +26,7 @@

import Foundation
import Testing
@testable import CotEditor
@testable import URLUtils

struct URLExtensionsTests {

Expand Down

0 comments on commit f6d31a9

Please sign in to comment.