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

Support user provided auto-complete #47

Merged
merged 1 commit into from Feb 19, 2021

Conversation

dbetter
Copy link

@dbetter dbetter commented Feb 19, 2021

  • Use by providing a user defined function with respect to text editor model and text positioning

Didn't want to spoil the current example but the following can be added as another example/extension of current one:
Provided SQL is the language of choice:

function createDependencyProposalsFrom(range: IRange) {
    // returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
    return [
      {
        label: '"TableA"',
        kind: languages.CompletionItemKind.Function,
        documentation: "TableA contains...",
        insertText: 'TableA',
        range: range
      },
      {
        label: '"TableA"',
        kind: languages.CompletionItemKind.Function,
        documentation: "TableA contains...",
        insertText: 'TableA',
        range: range
      }
    ];
  }


function createDependencyProposalsSelect(range: IRange) {
  // returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
  return [
    {
      label: '"column1"',
      kind: languages.CompletionItemKind.Function,
      documentation: "A very important database column containing....",
      insertText: 'column1',
      range: range
    },
    {
      label: '"column2"',
      kind: languages.CompletionItemKind.Function,
      documentation: "A very important database column containing....",
      insertText: 'column2',
      range: range
    }
  ];
}

function provideCompletionItems(model: editor.ITextModel | null | undefined, position: monaco.Position | null | undefined) {
  // find out if we are completing a property in the 'dependencies' object.
  if (!position) {
    return []
  }

  let wordAtPosition = model?.getWordUntilPosition(position);
  if (wordAtPosition?.word === '') {
    wordAtPosition = model?.getWordUntilPosition(position.delta(0, -1));
  }

  const matchFrom = wordAtPosition?.word.toLowerCase() === 'from';
  const matchSelect = wordAtPosition?.word.toLowerCase() === 'select';
  if (!matchFrom && !matchSelect) {
    return [];
  }

  const word = model?.getWordUntilPosition(position);
  const range = {
    startLineNumber: position.lineNumber,
    endLineNumber: position.lineNumber,
    startColumn: word?.startColumn,
    endColumn: word?.endColumn
  };

  const suggestions = matchFrom ? createDependencyProposalsFrom(range) : createDependencyProposalsSelect(range)
  return suggestions;
}

And usage would be:

<MonacoEditor
    ref={editorRef}
    height="500px"
    language={mode}
    autoComplete={(model, position) => provideCompletionItems(model, position)}
    value={code}
    editorDidMount={(editor) => editorDidMount(editor)}
    onChange={onChange}
    options={options}
/>

- Use by providing a user defined function with respect to text editor model and text positoning
@dbetter
Copy link
Author

dbetter commented Feb 19, 2021

@jaywcjlove - Would love it if you could go over this one. Please let me know if there are any questions.

@jaywcjlove
Copy link
Member

jaywcjlove commented Feb 19, 2021

@dbetter

This is a good idea. I have been trying to deal with it. My handling method package is too big.

https://github.com/jaywcjlove/nginx-editor

Is there a way to make it easier.

@jaywcjlove jaywcjlove merged commit fa3e2a4 into uiwjs:master Feb 19, 2021
@jaywcjlove
Copy link
Member

@dbetter Upgrade + @uiw/react-monacoeditor@3.2.0

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.

None yet

3 participants