如何在摩纳哥的自定义函数中自动填充参数?

时间:2018-02-01 11:23:23

标签: javascript autocomplete monaco-editor

我目前想要使用带有PHP的monaco编辑器,我希望使用自动完成功能扩展它以用于常规PHP函数以及自定义函数。

虽然功能的自动完成功能在我的早期原型上运行良好,但自动填充参数不起作用。请看小提琴的例子: https://jsfiddle.net/95aq6497/2/

目前,只有功能substr作为自动完成提供并且正在运行。但是,registerSignatureHelpProvider定义的参数的帮助程序不起作用:

 monaco.languages.registerSignatureHelpProvider('php', {
provideSignatureHelp: (model, position, token) => {
  console.log('Signature Help');
  return {
    activeParameter: 0,
    activeSignature: 0,
    signatures: [{
      label: 'string substr(string $string, int $start [, int $length])',
      parameters: [
        {
          label: 'string $string',
          documentation: 'The input string. Must be one character or longer.'
        },
        {
          label: 'int $start',
          documentation: "If $start is non-negative, the returned string will start at the $start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.\r\nIf $start is negative, the returned string will start at the $start'th character from the end of string. If $string is less than $start characters long, FALSE will be returned."
        },
        {
          label: 'int $length',
          documentation: 'If $length is given and is positive, the string returned will contain at most $length characters beginning from $start (depending on the length of $string) If $length is given and is negative, then that many characters will be omitted from the end of $string (after the start position has been calculated when a start is negative). If $start denotes the position of this truncation or beyond, FALSE will be returned. If $length is given and is 0, FALSE or NULL, an empty string will be returned. If $length is omitted, the substring starting from $start until the end of the string will be returned.'
        }
      ]
    }]
  };
}

});

该函数甚至没有执行,因为console.log根本没有触发。我错过了什么?类似地通过函数扩展javascript工作得很好,但它不适用于PHP,所以这里有什么不同?

1 个答案:

答案 0 :(得分:3)

您似乎没有提供signatureHelpTriggerCharacters选项:

monaco.languages.registerSignatureHelpProvider('php', {
  signatureHelpTriggerCharacters: ['(', ','],  <==================== this one
  provideSignatureHelp: (model, position, token) => {

https://jsfiddle.net/hec12da1/

要实现provideSignatureHelp功能,我建议你看看打字稿版本

https://github.com/Microsoft/monaco-typescript/blob/7027f0b4ba56fc0df136fb41791aa27e8c25ef54/src/languageFeatures.ts#L262