CodeEditSourceEditor icon indicating copy to clipboard operation
CodeEditSourceEditor copied to clipboard

Autocomplete Feature

Open FastestMolasses opened this issue 1 year ago • 3 comments

Description

Adds the first iteration of the autocomplete feature.

Related Issues

Checklist

  • [x] I read and understood the contributing guide as well as the code of conduct
  • [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

FastestMolasses avatar Dec 18 '24 07:12 FastestMolasses

@FastestMolasses hi! I forked from your branch and found a crash: StyledRangeStore+Internals.swift line 44 _guts[_guts.index(after: index)] fails with out of bounds error

this check fixes: let nextIndex = _guts.index(after: index) guard nextIndex < _guts.endIndex else { return }

dankinsoid avatar Mar 13 '25 16:03 dankinsoid

This might have been a bug that was fixed in a later commit. I'll merge main into this soon and check it out, this PR is currently on hold until the Language Server Installation PR gets merged.

FastestMolasses avatar Mar 14 '25 13:03 FastestMolasses

Hey @FastestMolasses was the status on the PR? Is it still up for review?

tom-ludwig avatar Apr 24 '25 10:04 tom-ludwig

@FastestMolasses hi! I forked from your branch and found a crash: StyledRangeStore+Internals.swift line 44 _guts[_guts.index(after: index)] fails with out of bounds error

this check fixes: let nextIndex = _guts.index(after: index) guard nextIndex < _guts.endIndex else { return }

@FastestMolasses is right, this was due to another component in the editor, which I believe I fixed.

thecoolwinter avatar Jun 18 '25 14:06 thecoolwinter

@FastestMolasses is this just waiting for a parameter to be able to pass in a suggestion delegate? I'd be happy to fill that out if that's all that's left.

thecoolwinter avatar Jun 18 '25 14:06 thecoolwinter

There's still the bug with the file contents not being updated. I have some refactors that fixes a few issues, I'll commit that and request reviews.

FastestMolasses avatar Jun 19 '25 03:06 FastestMolasses

@FastestMolasses if it's alright I'm going to push this one across the finish line. I'd like to get "Jump to Definition" going and I'll need the itembox for that too.

thecoolwinter avatar Jul 22 '25 14:07 thecoolwinter

No problem, committed my refactors, just needs a review.

FastestMolasses avatar Jul 23 '25 11:07 FastestMolasses

Ah shoot I didn't realize you had changes ready. I made a refactor making it so CESE controls displaying the autocomplete window and upgraded the code suggestion delegate to only need to reply with suggestions instead of managing windows too.

I could merge it here still, or I could open a PR to merge into this itembox branch to review first. Do you have a preference?

thecoolwinter avatar Jul 23 '25 15:07 thecoolwinter

This is what I came up with for the delegate:

`CodeSuggestionDelegate`

public protocol CodeSuggestionDelegate: AnyObject {
    func completionTriggerCharacters() -> Set<String>

    func completionSuggestionsRequested(
        textView: TextViewController,
        cursorPosition: CursorPosition
    ) async -> (windowPosition: CursorPosition, items: [CodeSuggestionEntry])?

    // This can't be async, we need it to be snappy. At most, it should just be filtering completion items
    func completionOnCursorMove(
        textView: TextViewController,
        cursorPosition: CursorPosition
    ) -> [CodeSuggestionEntry]?

    // Optional
    func completionWindowDidClose()

    func completionWindowApplyCompletion(
        item: CodeSuggestionEntry,
        textView: TextViewController,
        cursorPosition: CursorPosition
    )
    // Optional
    func completionWindowDidSelect(item: CodeSuggestionEntry)
}

public extension CodeSuggestionDelegate {
    func completionTriggerCharacters() -> Set<String> { [] }
    func completionWindowDidClose() { }
    func completionWindowDidSelect(item: CodeSuggestionEntry) { }
}

thecoolwinter avatar Jul 23 '25 15:07 thecoolwinter

I have the required changes for https://github.com/CodeEditApp/CodeEdit/pull/1949 too. It reduces the required loc for the AutoCompleteCoordinator significantly.

I'll commit my changes and we can revert if needed.

thecoolwinter avatar Jul 23 '25 15:07 thecoolwinter

I'd say this is ready to merge. @FastestMolasses I made changes but I can't request your review on your own pr so if you'd like changes please comment!

thecoolwinter avatar Jul 24 '25 16:07 thecoolwinter

I made one last change bringing the suggestion UI element down into CESE. The image and color determination are still up to the API caller, but this makes this component much easier to reuse.

I also figured out some more things about automatically setting the window and row size. The row size is now dynamic based on the user's selected font. It also updates the window size based on that dynamic row height.

The window's width is now determined by the smallest suggestion element's font width. This means it's dynamic down to a minimum size and up to a maximum size, similar to Xcode.

I also included a popover mode that this PR doesn't use but I'd already implemented it so I kept it in here.

thecoolwinter avatar Jul 24 '25 19:07 thecoolwinter

Looks good, lets merge it!

FastestMolasses avatar Jul 24 '25 23:07 FastestMolasses