monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

Cannot read property 'replace' of undefined

Open weinengjian opened this issue 8 years ago • 8 comments

when l type "entry" to complete the key word, it doesn't seem to work. I chose the "sql" language. Need l do some thing for this function?

errors.js:57 Uncaught Error: Cannot read property 'replace' of undefined

TypeError: Cannot read property 'replace' of undefined at Function.SnippetParser.escape (snippetParser.js:512) at SuggestController._onDidSelectItem (suggestController.js:222) at SuggestController. (suggestController.js:148) at Emitter.fire (event.js:115) at suggestWidget.js:401 at Function.SnippetParser.escape (snippetParser.js:512) at SuggestController._onDidSelectItem (suggestController.js:222) at SuggestController. (suggestController.js:148) at Emitter.fire (event.js:115) at suggestWidget.js:401 at errors.js:57

weinengjian avatar Nov 16 '18 03:11 weinengjian

@weinengjian , Seems like interface is changed.

var snippets = [{
	label: 'label'
	kind: monaco.languages.CompletionItemKind.Snippet,
	documentation: "some docu",
	insertText: 'your snippet value' // previously  {value: "snippet value"}
}]
monaco.languages.registerCompletionItemProvider('javascript', {
					provideCompletionItems: (modal, position) => {
						return {
							items: snippets, // bad hack
							suggestions: snippets
						}
					}
				});

this works for me. But if introduce ${1:name} and ${2:name} placeholder, tab is not stopping at the place holders. -thanks

yln99517 avatar Nov 16 '18 04:11 yln99517

@weinengjian Do you have a full reproducible example?

alexdima avatar Nov 16 '18 09:11 alexdima

Maybe the reason is my project is special and does not have the "webpack.config.js" file. So l can not edit it with ""plugins": [ new MonacoWebpackPlugin() ]"”.

weinengjian avatar Nov 16 '18 09:11 weinengjian

@alexandrudima Thank you for your help. l think it was the problem of my project's structure, and will find from other teams.

weinengjian avatar Nov 19 '18 01:11 weinengjian

I seem to have the same problem. My suggestion code looks like:

const defaultSuggestions = () => {
  return [];
};

const createPluckSuggestions = (node: any) => {
  if (!node) {
    return [];
  }
  return node.fields.map(f => {
    const documentation = f.annotations.find(a => a.key === "description");
    return {
      label: f.id,
      inserText: f.id,
      detail: f.ofType.text,
      documentation: documentation ? documentation.value : "",
      kind: monaco.languages.CompletionItemKind.Property
    };
  });
};

const createTypeSuggestions = (tags: any) => {
  const baseTypes = ["String", "Integer", "Decimal", "Boolean", "Date", "Time", "DateTime"];
  return [...Object.keys(tags), ...baseTypes].map(key => {
    return {
      label: key,
      inserText: key,
      kind: monaco.languages.CompletionItemKind.Property
    };
  });
};

export default () => {
  return {
    triggerCharacters: [".", ":"],
    provideCompletionItems: function(model, position) {
      const { tags = {} } = getState();
      var line = model
        .getValueInRange({
          startLineNumber: position.lineNumber,
          startColumn: 1,
          endLineNumber: position.lineNumber,
          endColumn: position.column
        })
        .trim();

      let pluckRegEx = RegExp(/^(pluck)( +)([A-Z][a-zA-Z0-9_]*)( *)(\.)/);
      const pluckResult = pluckRegEx.exec(line);
      if (pluckResult) {
        return {
          suggestions: createPluckSuggestions(tags[pluckResult[3]])
        };
      }

      let fieldRegEx = RegExp(/^([A-Z][a-zA-Z0-9_]*)( *)(:)( *)/);
      const fieldResult = fieldRegEx.exec(line);
      if (fieldResult) {
        return {
          suggestions: createTypeSuggestions(tags)
        };
      }

      return {
        suggestions: defaultSuggestions()
      };
    }
  };
};

The Error I get is:

TypeError: Cannot read property 'replace' of undefined
    at Function.e.escape (snippetParser.ts:522)
    at e._onDidSelectItem (suggestController.ts:243)
    at e.acceptSelectedSuggestion (suggestController.ts:372)
    at n.handler [as _callback] (suggestController.ts:492)
    at n.runEditorCommand (editorExtensions.ts:142)
    at editorExtensions.ts:165
    at e.invokeFunction (instantiationService.ts:54)
    at t.invokeWithinContext (codeEditorWidget.ts:353)
    at n.t.runCommand (editorExtensions.ts:158)
    at handler (editorExtensions.ts:90)

The version I'm using: "monaco-editor": "^0.15.6",

Extra information: Running in Electron and using TypeScript.

The intellisense pops up beautifully but when I select a value from the list I got the above mentioned error.

Baudin999 avatar Feb 21 '19 08:02 Baudin999

@yln99517 Have you got the snippet indicators $1 $2 working properly? Same issue here on v0.17.0 still. Indicators are not parsed.

@alexandrudima Hi could you please revisit this issue?

export const completionItemProvider = (monaco, id) => {
  const dependencyProposals = [{
    label: 'html',
    kind: monaco.languages.CompletionItemKind.Snippet,
    insertText: '<html>$1</html>',
    insertTextRules: monaco.languages.CompletionItemKind.InsertAsSnippet // not sure how this option works, given the limited documentation
  }]

  return {
    provideCompletionItems (model, position) {
      const textUntilPosition = model.getValueInRange({ startLineNumber: position.lineNumber, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column })
      const match = textUntilPosition.match(/\s*/)
      const suggestions = match ? dependencyProposals : []

      return { suggestions }
    }
  }
}

DMXL avatar Jul 04 '19 11:07 DMXL

@DMXL Your insertTextRules is incorrect. You need to use monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet.

Please try the code below in the Monaco Editor Playground.

@alexandrudima This question seems to pop up a lot so I think the example should be enhanced with a CompletionItem that includes a snippet.

function createProposals() {
    return [
        {
            label: 'html',
            kind: monaco.languages.CompletionItemKind.Snippet,
            insertText: '<html>$1</html>',
            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
        }
    ];
}

monaco.languages.registerCompletionItemProvider('json', {
    provideCompletionItems: function(model, position) {
        var suggestions = createProposals();
        return {
            suggestions: suggestions
        };
    }
});

monaco.editor.create(document.getElementById("container"), {
    value: "",
    language: "json"
});

rcjsuen avatar Jul 05 '19 12:07 rcjsuen

@rcjsuen Could you create a PR please?

alexdima avatar Jul 05 '19 20:07 alexdima