FIameCaster / prism-code-editor

Lightweight, extensible code editor component for the web using Prism
https://prism-code-editor.netlify.app
MIT License
43 stars 6 forks source link

Extension for code completion #18

Closed k1w1 closed 1 month ago

k1w1 commented 4 months ago

I am looking for a very light-weight code editor for a new language. prism-code-editor is very attractive because of the small size and the use of prism for easily defining syntax highlighting - especially compared to CodeMirror which has a very high bar to support new languages.

However there is a key feature I need which is code completion - i.e. a downdown list of possible completions at the current point in the code. I don't see any examples like that. Would such an extension be possible?

FIameCaster commented 4 months ago

This is something I've thought of adding in the past, but have never gotten around to. There's the addTooltip() utility (example on StackBlitz) that would make it easy to display the dropdown list itself. But there's obviously much more to handle than just placing the dropdown. Here's a list for simple code completion. You would need to:

None of these problems are hard to solve, but you might want something more sophisticated. For example returning no completions when inside a string or comment.

I am currently busy practising for exams, so I won't have any time to work on this before next month. What I'm thinking about making in the future is an extension that handles displaying, filtering, sorting and selecting the completions, where each language can define its own function that returns a list of completions based on the current state of the editor. It likely won't support tab stops initially.

If you want to implement code completion in your own projects, I hope this description helps. If not, would the proposed extension be sufficient for your needs?

k1w1 commented 4 months ago

Thanks for the thoughtful response. As you point out there are a lot of little behaviors to get right for completions to work well.

Starting with an extension that can display a list of completions, and leaving it to the application developer to determine what completions are currently relevant, seems like the best first step.

For my application I have my own parser and the document parse-tree available so I would want to use the application code to completely determine the list of available completions.

FIameCaster commented 4 months ago

For my application I have my own parser and the document parse-tree available so I would want to use the application code to completely determine the list of available completions

This would be no problem with the proposed extension. I'll begin working on it next month.

dmitrijsc commented 3 months ago

@FIameCaster I am looking forward to having the feature as well. Let me know if you need someone to test it.

panoply commented 2 months ago

Maybe there is some value to be taken from LSP completion item logic. Of course something much more simpler, but some basic expectations:

  1. Trigger characters (i.e, ", . etc)
  2. Completion List (i.e, datasets used for completions)

The work done here https://github.com/yuku/textcomplete may also be of value.

FIameCaster commented 1 month ago

@k1w1 @dmitrijsc

Good news, I have released a beta version with an experimental autocomplete extension. Check out the StackBlitz demo. It shows you how to add the extension and allows you to try it. If you want to try it locally, install the 3.4.0-beta.1 version from NPM.

It's somewhat lacking in features currently, but I am satisfied with what's implemented.

The following is planned for future beta versions:

Since this is a pre-release, everything is still subject to change. Any feedback would be appreciated.

dmitrijsc commented 1 month ago

@FIameCaster Looks great! I'll try it out during the week.

P.S. I'm planning to implement language support and completion for JSONata, probably by the end of August or September.