Autocomplete Feature
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 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 }
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.
Hey @FastestMolasses was the status on the PR? Is it still up for review?
@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.
@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.
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 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.
No problem, committed my refactors, just needs a review.
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?
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) { }
}
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.
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!
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.
Looks good, lets merge it!