Skip to content

TreeSitter Tag Auto Close #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1540"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6C1365292B8A7B94004A1D18"
BuildableName = "CodeEditSourceEditorExample.app"
BlueprintName = "CodeEditSourceEditorExample"
ReferencedContainer = "container:CodeEditSourceEditorExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6C1365292B8A7B94004A1D18"
BuildableName = "CodeEditSourceEditorExample.app"
BlueprintName = "CodeEditSourceEditorExample"
ReferencedContainer = "container:CodeEditSourceEditorExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6C1365292B8A7B94004A1D18"
BuildableName = "CodeEditSourceEditorExample.app"
BlueprintName = "CodeEditSourceEditorExample"
ReferencedContainer = "container:CodeEditSourceEditorExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ struct CodeEditSourceEditorExampleDocument: FileDocument {

static var readableContentTypes: [UTType] {
[
.sourceCode,
.plainText,
.delimitedText,
.script
.item
]
}

Expand Down
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
// A fast, efficient, text view for code.
.package(
url: "https://github.com/CodeEditApp/CodeEditTextView.git",
from: "0.7.3"
from: "0.7.4"
),
// tree-sitter languages
.package(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct CursorPosition: Sendable, Codable, Equatable {
/// - range: The range of the position.
/// - line: The line of the position.
/// - column: The column of the position.
init(range: NSRange, line: Int, column: Int) {
package init(range: NSRange, line: Int, column: Int) {
self.range = range
self.line = line
self.column = column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension TextViewController {

NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in
guard self.view.window?.firstResponder == self.textView else { return event }
let charactersIgnoringModifiers = event.charactersIgnoringModifiers
// let charactersIgnoringModifiers = event.charactersIgnoringModifiers
let commandKey = NSEvent.ModifierFlags.command.rawValue
let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue
if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ extension TextViewController {
// Filters

setUpOpenPairFilters(pairs: BracketPairs.allValues)
setUpTagFilter()
setUpNewlineTabFilters(indentOption: indentOption)
setUpDeletePairFilters(pairs: BracketPairs.allValues)
setUpDeleteWhitespaceFilter(indentOption: indentOption)
setUpTagFilter()
}

/// Returns a `TextualIndenter` based on available language configuration.
Expand Down Expand Up @@ -92,15 +92,13 @@ extension TextViewController {
}

private func setUpTagFilter() {
let filter = TagFilter(language: self.language.tsName)
textFilters.append(filter)
}

func updateTagFilter() {
textFilters.removeAll { $0 is TagFilter }

// Add new tagfilter with the updated language
textFilters.append(TagFilter(language: self.language.tsName))
guard let treeSitterClient, language.id.shouldProcessTags() else { return }
textFilters.append(TagFilter(
language: self.language,
indentOption: indentOption,
lineEnding: textView.layoutManager.detectedLineEnding,
treeSitterClient: treeSitterClient
))
}

/// Determines whether or not a text mutation should be applied.
Expand All @@ -123,30 +121,14 @@ extension TextViewController {
)

for filter in textFilters {
if let newlineFilter = filter as? NewlineProcessingFilter {
let action = mutation.applyWithTagProcessing(
in: textView,
using: newlineFilter,
with: whitespaceProvider, indentOption: indentOption
)
switch action {
case .none:
continue
case .stop:
return true
case .discard:
return false
}
} else {
let action = filter.processMutation(mutation, in: textView, with: whitespaceProvider)
switch action {
case .none:
continue
case .stop:
return true
case .discard:
return false
}
let action = filter.processMutation(mutation, in: textView, with: whitespaceProvider)
switch action {
case .none:
continue
case .stop:
return true
case .discard:
return false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TextViewController: NSViewController {
public var language: CodeLanguage {
didSet {
highlighter?.setLanguage(language: language)
updateTagFilter()
setUpTextFormation()
}
}

Expand Down

This file was deleted.

42 changes: 42 additions & 0 deletions Sources/CodeEditSourceEditor/Extensions/Node+filterChildren.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Node+filterChildren.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 5/29/24.
//

import SwiftTreeSitter

extension Node {
func firstChild(`where` isMatch: (Node) -> Bool) -> Node? {
for idx in 0..<childCount {
guard let node = child(at: idx) else { continue }
if isMatch(node) {
return node
}
}

return nil
}

func mapChildren<T>(_ callback: (Node) -> T) -> [T] {
var retVal: [T] = []
for idx in 0..<childCount {
guard let node = child(at: idx) else { continue }
retVal.append(callback(node))
}
return retVal
}

func filterChildren(_ isIncluded: (Node) -> Bool) -> [Node] {
var retVal: [Node] = []
for idx in 0..<childCount {
guard let node = child(at: idx) else { continue }
if isIncluded(node) {
retVal.append(node)
}
}

return retVal
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// NewlineProcessingFilter+TagHandling.swift
// CodeEditSourceEditor
//
// Created by Roscoe Rubin-Rottenberg on 5/19/24.
//

import Foundation
import TextStory
import TextFormation

// Helper extension to extract capture groups
extension String {
func groups(for regexPattern: String) -> [String]? {
guard let regex = try? NSRegularExpression(pattern: regexPattern) else { return nil }
let nsString = self as NSString
let results = regex.matches(in: self, range: NSRange(location: 0, length: nsString.length))
return results.first.map { result in
(1..<result.numberOfRanges).compactMap {
result.range(at: $0).location != NSNotFound ? nsString.substring(with: result.range(at: $0)) : nil
}
}
}
}
Loading