Fix Syntax code

This commit is contained in:
1024jp 2024-02-21 21:59:22 +09:00
parent b002f62714
commit f67c53020d
5 changed files with 24 additions and 24 deletions

View File

@ -161,7 +161,7 @@ final class EditorViewController: NSSplitViewController {
textView.syntaxKind = syntax.kind textView.syntaxKind = syntax.kind
textView.inlineCommentDelimiter = syntax.commentDelimiters.inline textView.inlineCommentDelimiter = syntax.commentDelimiters.inline
textView.blockCommentDelimiters = syntax.commentDelimiters.blockPair textView.blockCommentDelimiters = syntax.commentDelimiters.block
textView.syntaxCompletionWords = syntax.completionWords textView.syntaxCompletionWords = syntax.completionWords
} }

View File

@ -43,7 +43,7 @@ extension Syntax: Codable {
case comments case comments
case commentDelimiters case commentDelimiters
case outlines case outlines = "outlineMenu"
case completions case completions
case filenames case filenames
@ -139,7 +139,9 @@ extension Syntax.Highlight: Codable {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.begin, forKey: .begin) try container.encode(self.begin, forKey: .begin)
try container.encode(self.end, forKey: .end) if self.end?.isEmpty == false {
try container.encode(self.end, forKey: .end)
}
if self.isRegularExpression { if self.isRegularExpression {
try container.encode(true, forKey: .isRegularExpression) try container.encode(true, forKey: .isRegularExpression)
} }
@ -172,7 +174,7 @@ extension Syntax.Outline: Codable {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
self.pattern = try container.decode(String.self, forKey: .pattern) self.pattern = try container.decode(String.self, forKey: .pattern)
self.template = try container.decode(String.self, forKey: .template) self.template = try container.decodeIfPresent(String.self, forKey: .template) ?? ""
self.ignoreCase = try container.decodeIfPresent(Bool.self, forKey: .ignoreCase) ?? false self.ignoreCase = try container.decodeIfPresent(Bool.self, forKey: .ignoreCase) ?? false
self.bold = try container.decodeIfPresent(Bool.self, forKey: .bold) ?? false self.bold = try container.decodeIfPresent(Bool.self, forKey: .bold) ?? false
self.italic = try container.decodeIfPresent(Bool.self, forKey: .italic) ?? false self.italic = try container.decodeIfPresent(Bool.self, forKey: .italic) ?? false

View File

@ -80,12 +80,19 @@ struct Syntax: Equatable {
struct Comment: Equatable, Codable { struct Comment: Equatable, Codable {
private enum CodingKeys: String, CodingKey {
case inline = "inlineDelimiter"
case blockBegin = "beginDelimiter"
case blockEnd = "endDelimiter"
}
var inline: String? var inline: String?
var blockBegin: String? var blockBegin: String?
var blockEnd: String? var blockEnd: String?
var block: Pair<String>? {
var blockPair: Pair<String>? {
if let begin = self.blockBegin, let end = self.blockEnd { Pair(begin, end) } else { nil } if let begin = self.blockBegin, let end = self.blockEnd { Pair(begin, end) } else { nil }
} }
@ -206,7 +213,7 @@ struct Syntax: Equatable {
.mapValues { $0.compactMap { try? $0.extractor } } .mapValues { $0.compactMap { try? $0.extractor } }
.filter { !$0.value.isEmpty } .filter { !$0.value.isEmpty }
if let blockCommentDelimiters = self.commentDelimiters.blockPair { if let blockCommentDelimiters = self.commentDelimiters.block {
nestables[.pair(blockCommentDelimiters)] = .comments nestables[.pair(blockCommentDelimiters)] = .comments
} }
if let inlineCommentDelimiter = self.commentDelimiters.inline { if let inlineCommentDelimiter = self.commentDelimiters.inline {

View File

@ -24,8 +24,8 @@
// limitations under the License. // limitations under the License.
// //
import Combine
import Foundation import Foundation
import Combine
import AppKit.NSMenuItem import AppKit.NSMenuItem
import UniformTypeIdentifiers import UniformTypeIdentifiers
import Yams import Yams
@ -92,9 +92,8 @@ final class SyntaxManager: SettingFileManaging, ObservableObject {
// load bundled syntax list // load bundled syntax list
let url = Bundle.main.url(forResource: "SyntaxMap", withExtension: "json")! let url = Bundle.main.url(forResource: "SyntaxMap", withExtension: "json")!
let data = try! Data(contentsOf: url) let data = try! Data(contentsOf: url)
let maps = try! JSONDecoder().decode([SettingName: SyntaxMap].self, from: data) self.bundledMaps = try! JSONDecoder().decode([SettingName: SyntaxMap].self, from: data)
self.bundledMaps = maps self.bundledSettingNames = self.bundledMaps.keys.sorted(options: [.localized, .caseInsensitive])
self.bundledSettingNames = maps.keys.sorted(options: [.localized, .caseInsensitive])
// sanitize user setting file extensions // sanitize user setting file extensions
try? self.sanitizeUserSettings() try? self.sanitizeUserSettings()

View File

@ -46,10 +46,9 @@ final class SyntaxTests: XCTestCase {
try super.setUpWithError() try super.setUpWithError()
let bundle = Bundle(for: type(of: self)) let bundle = Bundle(for: type(of: self))
let urls = try XCTUnwrap(bundle.urls(forResourcesWithExtension: "yml", subdirectory: syntaxDirectoryName)) let urls = try XCTUnwrap(bundle.urls(forResourcesWithExtension: "yml", subdirectory: self.syntaxDirectoryName))
// load syntaxes // load syntaxes
let decoder = YAMLDecoder() let decoder = YAMLDecoder()
self.syntaxes = try urls.reduce(into: [:]) { (dict, url) in self.syntaxes = try urls.reduce(into: [:]) { (dict, url) in
let data = try Data(contentsOf: url) let data = try Data(contentsOf: url)
@ -57,12 +56,7 @@ final class SyntaxTests: XCTestCase {
dict[name] = try decoder.decode(Syntax.self, from: data) dict[name] = try decoder.decode(Syntax.self, from: data)
} }
self.htmlSyntax = try XCTUnwrap(self.syntaxes["HTML"])
// create HTML syntax
let htmlURL = try XCTUnwrap(urls.first { $0.lastPathComponent.contains("HTML") })
let string = try String(contentsOf: htmlURL)
let htmlDict = try XCTUnwrap(Yams.load(yaml: string) as? [String: Any])
self.htmlSyntax = Syntax(dictionary: htmlDict, name: "HTML")
XCTAssertNotNil(self.htmlSyntax) XCTAssertNotNil(self.htmlSyntax)
@ -98,11 +92,10 @@ final class SyntaxTests: XCTestCase {
let syntax = Syntax.none let syntax = Syntax.none
XCTAssertEqual(syntax.name, "None")
XCTAssertEqual(syntax.kind, .code) XCTAssertEqual(syntax.kind, .code)
XCTAssert(syntax.highlightParser.isEmpty) XCTAssert(syntax.highlightParser.isEmpty)
XCTAssertNil(syntax.commentDelimiters.inline) XCTAssertNil(syntax.commentDelimiters.inline)
XCTAssertNil(syntax.commentDelimiters.blockPair) XCTAssertNil(syntax.commentDelimiters.block)
} }
@ -110,10 +103,9 @@ final class SyntaxTests: XCTestCase {
let syntax = try XCTUnwrap(self.htmlSyntax) let syntax = try XCTUnwrap(self.htmlSyntax)
XCTAssertEqual(syntax.name, "HTML")
XCTAssertFalse(syntax.highlightParser.isEmpty) XCTAssertFalse(syntax.highlightParser.isEmpty)
XCTAssertNil(syntax.commentDelimiters.inline) XCTAssertNil(syntax.commentDelimiters.inline)
XCTAssertEqual(syntax.commentDelimiters.blockPair, Pair("<!--", "-->")) XCTAssertEqual(syntax.commentDelimiters.block, Pair("<!--", "-->"))
} }
@ -123,7 +115,7 @@ final class SyntaxTests: XCTestCase {
let source = try XCTUnwrap(self.htmlSource) let source = try XCTUnwrap(self.htmlSource)
let textStorage = NSTextStorage(string: source) let textStorage = NSTextStorage(string: source)
let parser = SyntaxParser(textStorage: textStorage, syntax: syntax) let parser = SyntaxParser(textStorage: textStorage, syntax: syntax, name: "HTML")
// test outline parsing with publisher // test outline parsing with publisher
let outlineParseExpectation = self.expectation(description: "didParseOutline") let outlineParseExpectation = self.expectation(description: "didParseOutline")