CodeMirror - Issue with setting AutoComplete "options" correctly

I've been using CodeMirror and trying to style the autocomplete pop up with CSS. It's been a challenge because the pop up disappears when I inspect styles and make changes.

I came across some code in show-hint.js that looks like this:

if (options.closeOnUnfocus !== false) {
    var closingOnBlur;
    cm.on("blur", this.onBlur = function () { closingOnBlur = setTimeout(function () { completion.close(); }, 100); });
    cm.on("focus", this.onFocus = function () { clearTimeout(closingOnBlur); });
}

Commenting out the above code prevents the pop up from disappearing when I click on other elements, which is what I wanted. But now I'm trying to figure out how to toggle this behavior on and off.

I wanted to set the closeOnUnfocus option on my own, but I couldn't find a way to do it. Further research led me to an example on the CodeMirror website with the following code:

  CodeMirror.commands.autocomplete = function(cm) {
    CodeMirror.showHint(cm, CodeMirror.hint.anyword);
  }

Looking deeper, show-hint.js has a function named showHint with the following signature:

CodeMirror.showHint = function (cm, getHints, options) {
    if (cm.somethingSelected()) return;
    if (getHints == null) {
        if (options && options.async) return;
        else getHints = CodeMirror.hint.auto;
    }

    if (cm.state.completionActive) cm.state.completionActive.close();

    var completion = cm.state.completionActive = new Completion(cm, getHints, options || {});
    CodeMirror.signal(cm, "startCompletion", cm);
    if (completion.options.async)
        getHints(cm, function (hints) { completion.showHints(hints); }, completion.options);
    else
        return completion.showHints(getHints(cm, completion.options));
};

I tried passing my option through this function by modifying the autocomplete command like this:

CodeMirror.commands.autocomplete = function (cm) {
    CodeMirror.showHint(cm, CodeMirror.hint.anyword, {
        closeOnUnfocus: false
    });
}

However, this approach didn't work as expected - it seems that the options are not being passed through at all. Even when I log the options in show-hint.js, they are ignored. I'm confused about how to pass options through successfully. Can anyone help me with this?

Answer №1

To customize the appearance of the hint menu, simply utilize the provided CSS hooks instead of tinkering with the autocomplete handlers. For example:

.CodeMirror-hints {
    background-color: red;
}
.CodeMirror-hint {
    background-color: green;
}
.CodeMirror-hint-active {
    background-color: blue;
    color: yellow;
}

Take a look at this interactive demo to see it in action.

Answer №2

Recently I started using Codemirror version 4.1 and encountered a similar issue. Upon examining the show-hint.js file, it became evident that the documentation is outdated.

To retrieve suggestions, try utilizing the following code snippet:

CodeMirror.showHint({hint: CodeMirror.hint.deluge, completeSingle: false, closeOnUnfocus: true});

If you require asynchronous suggestion retrieval (which was the case for me), make sure to include the following code snippet before the previous one:

CodeMirror.hint.deluge.async = true;

I hope this information proves helpful!

Answer №3

To include the options, use the following method:

CodeMirror.showHint(cm,CodeMirror.hint.anyword,{completeSingle: false,closeOnUnfocus:false});

To implement the code, follow these steps:

editor.on("keyup",function(cm){ 
    CodeMirror.showHint(cm,CodeMirror.hint.deluge,{completeSingle: false});
  });

This approach has been effective for me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance the user experience by adding visual appeal to the first option in the selection form. Make it

Is there a way to change the color of the first option in the selection box to grey, similar to the example above? Additionally, how can I create a clickable icon that will display all options? The current setup is not functioning correctly. <div clas ...

Is there a way for an input form to completely fill a table data cell without increasing the width of the entire row?

I would like to add a row of text inputs at the bottom of my table, with each input having the same size as the widest element in their respective rows. I have tried setting the width to 100%, but this ends up expanding the entire row, which is not the des ...

What is the best way to center the startIcon material ui icon?

I'm having trouble keeping the icon button centered on my page. When I add left: "0.5rem" to the sx prop, it ends up pushing the button icon even further away from center. I'd prefer not to use makeStyles to manually override the position. Any ad ...

What is the best way to vertically center content in Bootstrap 4.5?

Below is a snippet of the code I've been working on. <body style="height: 100vh; margin: 0; padding: 0; "> <!-- Form Section ============================================= --> <p>[wpshortcode1]</p> <div cl ...

How can CSS files be shared among multiple projects within Visual Studio 2012?

I am working on a Visual Studio Project Solution that includes 3 separate projects. To streamline the process and avoid duplication, I aim to have a shared CSS file for all 3 projects since they all follow the same CSS rules. Despite trying the Add As Lin ...

What might be causing the in-viewport javascript to not work in my code?

Why is my in-viewport JavaScript code not functioning properly? Link to JSFiddle code When the Click to move button is clicked, the cat image will slide correctly. However, when implementing the following code: if($("#testtest").is(":in-viewport")) ...

What is the best method to eliminate unnecessary space between controls in HTML?

Presently, I am facing the following scenario. http://jsfiddle.net/ef7vh/1/ </p> <pre> <div> <button>Button1</button> <span style="border: solid 1px black; padding: 0px 10px 0px 10px">My Text</span> < ...

assigning a CSS class to an input element based on a certain condition

My dilemma involves an input field for which I need to conditionally apply a CSS class. For instance, if firstName == undefined, I want to apply the CSS class ng-dirty. To achieve this, I attempted: <input required [(ngModel)]="customer.firstName" nam ...

What is causing my page to automatically refresh whenever I click on buttons?

The code snippet below shows the structure of my webpage : HTML : <button class="add-col"><i class="fas fa-columns"> Add a column</i></button> <div class="table-responsive"> <table class="table"> ...

Issue with PHP and CSS: Navigation bar does not properly indicate the current active tab

When attempting to display a webpage with a tabbed style navbar that changes upon clicking, I am encountering an issue. The desired result is shown in this image: https://i.sstatic.net/xR9zw.png However, after clicking on the links multiple times, the ou ...

Semi-circle border using CSS

Looking to replicate the circular design shown in the image using CSS. Any tips on achieving this? ...

Can someone explain why the color of a <select> element in Chrome is being influenced by the CSS property "font-family: inherit"? Here is an example

I've always been curious as to why the <select> element appears in different colors on various web pages. I've noticed that when the font-family:inherit css property affects the select element, it ends up looking blue! <!-- BLACK --> ...

A versatile Material UI paper that adjusts its dimensions dynamically

Utilizing Material-UI's Paper component (https://mui.com/components/paper/), I've encountered a scenario where the content within the Paper element needs to be dynamic. <Paper className="modal" elevation={3}> ...Content </Paper ...

Ensuring the same height for the navigation bar and logo with aligned vertical text

I'm in the process of recreating a simple navigation menu and I've reached the section where I am encountering an issue with the list items. The orange li element is not filling up 100% of the width, and I also need the links to align in the midd ...

Combining hover and mousedown event listeners in jQuery: Tips and tricks

htmlCODE: <div class="original_circle" style="border-radius: 50%; background-color: blue; width: 40px; height:40px; z-index: 0"> <div class="another_circle" style="position:absolute; border-radius: 50%; background-color: coral; width: 40px; h ...

Display a unique and unconventional image as a dynamic full-screen background, ensuring the entire image is visible using React/RedwoodJS

Looking for a somewhat unusual solution here - I want to use an image as a background with all edges of the picture visible at all times. Typically, I would use the "cover" setting for this, but that doesn't achieve what I need (image included). After ...

Margins Disrupt Proper Text Alignment;

(If you know of a simpler solution to this issue, please share!) I am aiming for this website to consist of two text sections, one on each side of the screen. I managed to achieve this successfully by using text alignment and translate3d to shift the tex ...

Restore the initial content of the div element

Currently, I am utilizing the .hide() and .show() functions to toggle the visibility of my page contents. Additionally, I am using the .HTML() function to change the elements inside a specific div. $('#wrap').html(' <span id="t-image"> ...

Adjusting the color of an element in ReactJS as the user scrolls and hits a specific position

Is there a way to dynamically change the color of my header based on the background color as I scroll through different sections of my webpage? I have a fixed header and multiple sections with varying background colors. I want the header's text color ...

Display picture next to content with the help of CSS/Bootstrap and Django's Jinja 2

I'm having trouble displaying an image next to text, it keeps showing up below. Here's the code snippet: <div class="container"> <div class="row"> <div class="col-md-8"> < ...