Skip to content
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

When a range replaced with a string, the cursor is not at the end of the newly replaced string. #24

Open
kelalaka153 opened this issue Apr 23, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@kelalaka153
Copy link

I'm using the below function to insert the text at the current selection ( current cursor position is enough for me, though)

func insertText(_ text: String, at range: Range<String.Index>, in string: String) -> String {
        var newString = string
        newString.replaceSubrange(range, with: text)
        return newString
    }

With the help of the selection binding, the text insertion is quite easy ( thanks for that), however, the cursor stays at the beginning of the inserted text ( the one that replaced the text of the range).

In the usual approach, on the other hand, the cursor must be placed at the end of newly inserted text ( when the range size is 0)

If I select a range more than the cursor position, this time, there is no cursor but only a selection that has the same size of the replaced text. I was expecting that the selection after the text inserts keep the size of the replaced part.

Ok, this may be the expected behavior since we may just be replacing the text that the editor's string with a new value.

Is there a way to achieve the text insertion so that the cursor moved to the end of the inserted string? This is necessary for me. Maybe there is a workaround for this one can move the cursor to the x position.


P.S. Overall, it is a great CodeEditor.

@kelalaka153
Copy link
Author

Ok, I've solved my issue.

func insertText(_ text: String, at range: Range<String.Index>, in string: String) -> String {
        
        let rangeSize = source.distance(from: range.lowerBound, to: range.upperBound)

        let diff = text.count - rangeSize

        var newString = string
        newString.replaceSubrange(range, with: text)

        var startIndex = newString.index(newString.startIndex, offsetBy: 0 )
        var endIndex = startIndex

        if rangeSize == 0 {
            startIndex = newString.index(range.lowerBound, offsetBy: diff )
            endIndex = newString.index(range.upperBound, offsetBy: diff)

        } else {
            startIndex = newString.index(range.lowerBound, offsetBy: 0 )
            if diff >= 0 {
                endIndex = newString.index(range.upperBound, offsetBy: diff)
            } else {
                endIndex = newString.index(range.lowerBound, offsetBy: text.count)
            }
        }
        selection = startIndex..<endIndex
       
        return newString

Hope this helps whoever needs.

@helje5
Copy link
Member

helje5 commented May 1, 2023

Looks good, want to put this in a pull request?

@helje5 helje5 reopened this May 1, 2023
@helje5 helje5 added the bug Something isn't working label May 1, 2023
@kelalaka153
Copy link
Author

As far as I remember, system's copy past works flawlessly on the editor ( tested now to make sure that). I needed this to insert/replace some text within my application.

I don't think that the CodeEditor need such internal functionality, or am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants