-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 7 fully featured languages to CodeEditor.
* Supports syntax highlighting, code completion, find and replace, text diff, validation, minimap, inline linter messaging, bracket matching for C, C++, JSON, Python, Rust, Swift, and TOML. * TODO: Create a (.usda) tree sitter to allow for the above to work in a fully featured usd text editor.
- Loading branch information
Showing
178 changed files
with
1,676,704 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Sources/Editors/Code/CosmoEditor/Controller/CursorPosition.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* -------------------------------------------------------------- | ||
* :: : K R A K E N : :: | ||
* -------------------------------------------------------------- | ||
* @wabistudios :: metaverse :: kraken | ||
* | ||
* This program is free software; you can redistribute it, and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Check out | ||
* the GNU General Public License for more details. | ||
* | ||
* You should have received a copy for this software license, the | ||
* GNU General Public License along with this program; or, if not | ||
* write to the Free Software Foundation, Inc., to the address of | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (C) 2023 Wabi Foundation. | ||
* All Rights Reserved. | ||
* -------------------------------------------------------------- | ||
* . x x x . o o o . x x x . : : : . o x o . : : : . | ||
* -------------------------------------------------------------- */ | ||
|
||
import Foundation | ||
|
||
/// # Cursor Position | ||
/// | ||
/// Represents the position of a cursor in a document. | ||
/// Provides information about the range of the selection relative to the document, and the line-column information. | ||
/// | ||
/// Can be initialized by users without knowledge of either column and line position or range in the document. | ||
/// When initialized by users, certain values may be set to `NSNotFound` or `-1` until they can be filled in by the text | ||
/// controller. | ||
/// | ||
public struct CursorPosition: Sendable, Codable, Equatable | ||
{ | ||
/// Initialize a cursor position. | ||
/// | ||
/// When this initializer is used, ``CursorPosition/range`` will be initialized to `NSNotFound`. | ||
/// The range value, however, be filled when updated by ``CosmoEditor`` via a `Binding`, or when it appears | ||
/// in the``TextViewController/cursorPositions`` array. | ||
/// | ||
/// - Parameters: | ||
/// - line: The line of the cursor position, 1-indexed. | ||
/// - column: The column of the cursor position, 1-indexed. | ||
public init(line: Int, column: Int) | ||
{ | ||
range = .notFound | ||
self.line = line | ||
self.column = column | ||
} | ||
|
||
/// Initialize a cursor position. | ||
/// | ||
/// When this initializer is used, both ``CursorPosition/line`` and ``CursorPosition/column`` will be initialized | ||
/// to `-1`. They will, however, be filled when updated by ``CosmoEditor`` via a `Binding`, or when it | ||
/// appears in the ``TextViewController/cursorPositions`` array. | ||
/// | ||
/// - Parameter range: The range of the cursor position. | ||
public init(range: NSRange) | ||
{ | ||
self.range = range | ||
line = -1 | ||
column = -1 | ||
} | ||
|
||
/// Private initializer. | ||
/// - Parameters: | ||
/// - 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) | ||
{ | ||
self.range = range | ||
self.line = line | ||
self.column = column | ||
} | ||
|
||
/// The range of the selection. | ||
public let range: NSRange | ||
/// The line the cursor is located at. 1-indexed. | ||
/// If ``CursorPosition/range`` is not empty, this is the line at the beginning of the selection. | ||
public let line: Int | ||
/// The column the cursor is located at. 1-indexed. | ||
/// If ``CursorPosition/range`` is not empty, this is the column at the beginning of the selection. | ||
public let column: Int | ||
} |
87 changes: 87 additions & 0 deletions
87
Sources/Editors/Code/CosmoEditor/Controller/TextViewController+Cursor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* -------------------------------------------------------------- | ||
* :: : K R A K E N : :: | ||
* -------------------------------------------------------------- | ||
* @wabistudios :: metaverse :: kraken | ||
* | ||
* This program is free software; you can redistribute it, and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Check out | ||
* the GNU General Public License for more details. | ||
* | ||
* You should have received a copy for this software license, the | ||
* GNU General Public License along with this program; or, if not | ||
* write to the Free Software Foundation, Inc., to the address of | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (C) 2023 Wabi Foundation. | ||
* All Rights Reserved. | ||
* -------------------------------------------------------------- | ||
* . x x x . o o o . x x x . : : : . o x o . : : : . | ||
* -------------------------------------------------------------- */ | ||
|
||
import AppKit | ||
import Foundation | ||
|
||
extension TextViewController | ||
{ | ||
/// Sets new cursor positions. | ||
/// - Parameter positions: The positions to set. Lines and columns are 1-indexed. | ||
public func setCursorPositions(_ positions: [CursorPosition]) | ||
{ | ||
if isPostingCursorNotification { return } | ||
var newSelectedRanges: [NSRange] = [] | ||
for position in positions | ||
{ | ||
let line = position.line | ||
let column = position.column | ||
guard (line > 0 && column > 0) || (position.range != .notFound) else { continue } | ||
|
||
if position.range == .notFound | ||
{ | ||
if textView.textStorage.length == 0 | ||
{ | ||
// If the file is blank, automatically place the cursor in the first index. | ||
newSelectedRanges.append(NSRange(location: 0, length: 0)) | ||
} | ||
else if let linePosition = textView.layoutManager.textLineForIndex(line - 1) | ||
{ | ||
// If this is a valid line, set the new position | ||
let index = linePosition.range.lowerBound + min(linePosition.range.upperBound, column - 1) | ||
newSelectedRanges.append(NSRange(location: index, length: 0)) | ||
} | ||
} | ||
else | ||
{ | ||
newSelectedRanges.append(position.range) | ||
} | ||
} | ||
textView.selectionManager.setSelectedRanges(newSelectedRanges) | ||
} | ||
|
||
/// Update the ``TextViewController/cursorPositions`` variable with new text selections from the text view. | ||
func updateCursorPosition() | ||
{ | ||
var positions: [CursorPosition] = [] | ||
for selectedRange in textView.selectionManager.textSelections | ||
{ | ||
guard let linePosition = textView.layoutManager.textLineForOffset(selectedRange.range.location) | ||
else | ||
{ | ||
continue | ||
} | ||
let column = (selectedRange.range.location - linePosition.range.location) + 1 | ||
let row = linePosition.index + 1 | ||
positions.append(CursorPosition(range: selectedRange.range, line: row, column: column)) | ||
} | ||
|
||
isPostingCursorNotification = true | ||
cursorPositions = positions.sorted(by: { $0.range.location < $1.range.location }) | ||
NotificationCenter.default.post(name: Self.cursorPositionUpdatedNotification, object: nil) | ||
isPostingCursorNotification = false | ||
} | ||
} |
Oops, something went wrong.