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

feat: Autocomplete using parameter objects #410

Merged
merged 11 commits into from
Feb 12, 2024

Conversation

Mitcheljager
Copy link
Owner

@Mitcheljager Mitcheljager commented Feb 10, 2024

This PR adds an options to the settings dropdown to autocomplete using parameter objects. All autocompletes will use parameter objects instead of their regular format. When you change the settings the completions map is regenerated with the new corresponding format.
Along with that settings there's a setting to only do this if the completion has more than x arguments, defaulting to 2. If it has less than the given amount the regular format will be used. This is mostly to prevent silly parameter objects like Button({ Button: Melee }).
param-objects-autocomplete

Since the autocompletions can be multi line I added a function that transforms transactions in CodeMirror that contain new lines to use the expected amount of tabs. An added benefit of this is that this fixes a bug with pasting code that contains tabs that would only be indented correctly on the first line

Before After
paste-before paste-after

Copy link
Contributor

@netux netux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good! The indentation changes don't work so well, but the autocomplete is greatly appreciated 🙂

Comment on lines 298 to 299
let tabs = ""
for (let i = 0; i < indents; i++) { tabs += "\t" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let tabs = ""
for (let i = 0; i < indents; i++) { tabs += "\t" }
const tabs = "\t".repeat(indents)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, neat!

"word-wrap": false
"word-wrap": false,
"autocomplete-parameter-objects": false,
"autocomplete-min-parameter-size": 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says 3, but this is set to 2. Which one should it be?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, it should be 2. I had it at 3 initially, but 2 felt better

if (transaction.transactions.every(tr => ["input.paste", "input.complete"].includes(tr.annotation(Transaction.userEvent)))) {
const [range] = transaction.changedRanges
const text = transaction.state.doc.toString().slice(range.fromB, range.toB)
const indents = getIndentForLine(view.state, range.fromB)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is not all sunshine and rainbows, sadly

brave_IJ8rPnV7Up.mp4

I think this could benefit from reapplying indentation on each line of the pasted content?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some improvements and it should be much better. It's not quite perfect but the imperfections make enough sense. VSCode has similar (but different) inconsistencies. I think this is about as close as we can get without fully parsing the text, which would be too slow for larger texts anyway.

const useParameterObject = $settings["autocomplete-parameter-objects"] && params.args_length >= $settings["autocomplete-min-parameter-size"]
const apply = v.args.map(a => {
let string = a.default?.toString().toLowerCase().replaceAll(",", "")
if (useParameterObject) string = `\n\t${ a.name }: ${ string }`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a limit on when each parameter is put on a new line and when not?

If I was one of those people that put autocomplete-min-parameter-size to 1, maybe I'd like for autocomplete to not expand the parameter object into multiple lines if the parameter amount is <3 or something.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

min-newline

@Mitcheljager Mitcheljager merged commit ce5b715 into master Feb 12, 2024
9 checks passed
@Mitcheljager Mitcheljager deleted the feat/autocomplete-param-objects branch February 12, 2024 17:27
Loottoo added a commit to Loottoo/workshop.codes that referenced this pull request Feb 12, 2024
Since commit Mitcheljager#410 there is an error when copypasting a single line (no problem with more than 1 line):
"Uncaught RangeError: Invalid count value: -2"
Adding Math.max() as a temporary solution, not sure what is causing it?

Adding back a bugfix (prevent auto-indents on enter from adding up infinitely when the cursor is outside of an open bracket) that disappeared with the change to some of the functions

added a selectionDirection constant to prevent the selection from flipping around when tabbing (cursor used to always jump to 'to' position, even when it started at 'from')
Mitcheljager added a commit that referenced this pull request Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants