Skip to content

Commit

Permalink
TreeSitter Tag Auto Close (#250)
Browse files Browse the repository at this point in the history
### Description

Fixes a few bugs with #247 and converts it to use tree-sitter rather
than a regex-based implementation. This should be faster on larger
documents and makes it more robust to edge cases in tag regexes. This
also handles newlines correctly, as the old PR caused the editor to no
longer be able to delete newlines

Also fixes a small bug in the `TreeSitterClient` that caused *every*
query to be dispatched to main asynchronously. This was the cause for a
few visual oddities like flashing colors when changing themes. This also
improves highlighting while scrolling fast as most highlights are
processed synchronously.

- Removes extensions on `NewlineProcessingFilter`
- Cleans up `TagFilter`
  - Moves all newline processing to the one filter
- Use tree-sitter for tag completion, supporting the following
languages: HTML, JSX, TSX
- Adds a few methods to `TreeSitterClient` for synchronously querying
the tree sitter tree.
- Adds a new `TreeSitterClientExecutor` class that the client uses to
execute operations safely asynchronously and synchronously.
- This is extremely useful for testing, as it allows the tests to force
all operations to happen synchronously.
- Adds a check to `dispatchMain` to see if the thread is already the
main thread (meaning no async dispatch)

### Related Issues

* #244 
* Discussion on discord
[Here](https://discord.com/channels/951544472238444645/1242238782653075537)

### Checklist

- [x] I read and understood the [contributing
guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md)
as well as the [code of
conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md)
- [x] The issues this PR addresses are related to each other
- [x] My changes generate no new warnings
- [x] My code builds and runs on my machine
- [x] My changes are all related to the related issue above
- [x] I documented my code

### Screenshots


https://github.com/CodeEditApp/CodeEditSourceEditor/assets/35942988/8fc559a4-15c9-4b4e-a3aa-57c86c57f7c9


https://github.com/CodeEditApp/CodeEditSourceEditor/assets/35942988/a209b40f-7aa3-4105-aa37-5608e8b4bcdb
  • Loading branch information
thecoolwinter authored May 30, 2024
1 parent 1740482 commit cf85789
Show file tree
Hide file tree
Showing 22 changed files with 893 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/CodeEditApp/CodeEditTextView.git",
"state" : {
"revision" : "86b980464bcb67693e2053283c7a99bdc6f358bc",
"version" : "0.7.3"
"revision" : "80911be6bcdae5e35ef5ed351adf6dda9b57e555",
"version" : "0.7.4"
}
},
{
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/CodeEditApp/CodeEditTextView.git",
"state" : {
"revision" : "86b980464bcb67693e2053283c7a99bdc6f358bc",
"version" : "0.7.3"
"revision" : "80911be6bcdae5e35ef5ed351adf6dda9b57e555",
"version" : "0.7.4"
}
},
{
Expand Down
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

0 comments on commit cf85789

Please sign in to comment.