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

Is there are way to access EditorState? #19

Closed
gavinmcfarland opened this issue Jul 12, 2023 · 8 comments
Closed

Is there are way to access EditorState? #19

gavinmcfarland opened this issue Jul 12, 2023 · 8 comments

Comments

@gavinmcfarland
Copy link

Firstly. Thank you for sharing this Svelte component. It makes it a lot easier to use CodeMirror out of the box.

I'm trying to add my own autocomplete suggestions but I'm struggling to understand CodeMirror's documentation. If I understand it correctly, EditorState is needed to pass through as a CompletionContext. If this is correct is there a way to get the EditorState from svelte-codemirror-editor?

https://codemirror.net/docs/ref/#autocomplete.CompletionContext

@SomaticIT
Copy link
Contributor

Hello,

If I remember correctly, you never have to instanciate the CompletionContext class by yourself.
CodeMirror is providing you the CompletionContext instance to your language module.

@gavinmcfarland
Copy link
Author

gavinmcfarland commented Jul 13, 2023

Ah ok, thanks. I'll have another look. Do you know what this might happen to look like? I'm guessing maybe something like this now?

<script>
function myCompletions(context) {
    let word = context.matchBefore(/\w*/)
    if (word.from == word.to && !context.explicit) {
        return null
    }
    return {
        from: word.from,
            options: [
                {label: "match", type: "keyword"},
                {label: "hello", type: "variable", info: "(World)"},
                {label: "magic", type: "text", apply: "⠁⭒*.✩.*⭒⠁", detail: "macro"}
            ]
      }
}
</script>

<CodeMirror
    bind:value={value}
    lang={javascript().data.of({
        autocomplete: myCompletions
    })}
    extensions={[autocomplete()]}
/>

@SomaticIT
Copy link
Contributor

Sorry for my late response.
It seems correct.

Did it work for you?

@SomaticIT
Copy link
Contributor

For information, PR #31 allows you to get access to EditorView using ready event.
Any news to share for your use case?

@gavinmcfarland
Copy link
Author

Hi. Thanks for letting me know about this. The code I proposed didn't work unfortunately so I created an instance from scratch. When I get chance I'll try the new event. Thanks you!

@yashash-pugalia
Copy link

@gavinmcfarland could you please share the code if you could get that to work.

@gavinmcfarland
Copy link
Author

Sure, I created it from scratch. Maybe it'll help you.

https://github.com/gavinmcfarland/figma-vite-svelte-codemirror-example/blob/main/src/App.svelte

@yashash-pugalia
Copy link

Thanks a lot Gavin, really appreciate it.

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

No branches or pull requests

3 participants