I have been trying to find a solution to implement richer autocompletion for python for quite some time now, and I have not been able to do it yet. I would like to achieve the same autocompletion result as javascript. I thought it should be easy, but I started to come across articles that say that you need to deploy a language server for this purpose. Is there really no simpler way to achieve autocompletion (for example, to display a list of methods for a class created by the user)? I found the following method (which, by the way, does not work quite correctly and, in addition, all the hints must be implemented manually):
import { FC, useEffect } from "react";
import * as monaco from "monaco-editor";
function createDependencyProposals(range) {
// returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
// here you could do a server side lookup
return [
{
label: "test",
insertText: "test",
range: range,
},
];
}
export const MonacoEditorComponent: FC = () => {
let flag = false;
useEffect(() => {
if (!flag) {
monaco.languages.registerCompletionItemProvider("python", {
provideCompletionItems: function (model, position) {
const word = model.getWordUntilPosition(position);
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn,
};
return {
suggestions: createDependencyProposals(range),
suggestions: [],
};
},
});
monaco.editor.create(document.getElementById("container"), {
value: "def something():ntreturn 5nn",
language: "python",
});
}
flag = true;
}, []);
return <div id="container" style={{ width: 800, height: 400 }}></div>;
};