简体   繁体   English

如何使用 Javascript 删除 Monaco Editor 的自动完成功能?

[英]How to remove autocompletions for Monaco Editor using Javascript?

I'm embedding the Monaco Editor in my App, I have some javascript files that should not show completions for "Web" environments (think Node.js or similar) I would like to have completions appear for only the functions and classes that are defined on the page.我在我的应用程序中嵌入了摩纳哥编辑器,我有一些 javascript 文件不应该显示“Web”环境的完成(想想 Node.js 或类似的)我希望只为在页。

How do I remove all the "Web" autocompletions from the javascript mode?如何从 javascript 模式中删除所有“Web”自动完成?

在此处输入图像描述

This is a great question.这是一个很好的问题。 This Github issue covers the same topic: https://github.com/Microsoft/monaco-editor/issues/61 .这个 Github 问题涵盖了相同的主题: https : //github.com/Microsoft/monaco-editor/issues/61

According to that, disabling the built-in library is done by passing noLib to setCompilerOptions:据此,禁用内置库是通过将 noLib 传递给 setCompilerOptions 来完成的:

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({ noLib: true, allowNonTsExtensions: true });

You can add additional typings by using addExtraLib, which hangs off of the javascriptDefaults.您可以使用 addExtraLib 添加其他类型,它挂在 javascriptDefaults 之外。 Typescript provides some d.ts files that provide the basics for ES5 and ES6, without the browser cruft. Typescript 提供了一些 d.ts 文件,这些文件提供了 ES5 和 ES6 的基础知识,没有浏览器的缺陷。 Download one of them and find a way to include it in your project.下载其中一个并找到一种方法将其包含在您的项目中。 You will need to pass it in as a string.您需要将其作为字符串传递。

var coreDefsName = 'lib.es5.d.ts';

// Import the text of your chosen def file.  This will depend on your bundler.
var coreDefs = getResourceString('./' + coreDefsName);

// Register the additional library.
monaco.languages.typescript.javascriptDefaults.addExtraLib(
coreDefs,
coreDefsName
);

There is another way other than noLibs .除了noLibs之外,还有另一种方式。 You can also use libs to define the default libs you want without removing all of the default libs.您还可以使用libs定义您想要的默认库,而无需删除所有默认库。 For example, you may still want the ES6 lib.例如,您可能仍然需要 ES6 库。

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  target: monaco.languages.typescript.ScriptTarget.ES2015,
  lib: ["es6"]
});

From the Typescript docs in the CompilerOptions section:来自 CompilerOptions 部分的 Typescript 文档:

Note:笔记:

If --lib is not specified a default list of libraries are injected.如果未指定--lib,则会注入默认的库列表。 The default libraries injected are: For --target ES5: DOM,ES5,ScriptHost For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost注入的默认库是: 对于 --target ES5: DOM,ES5,ScriptHost 对于 --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

What you probably want to do is omit the DOM library.您可能想要做的是省略DOM库。

Make sure you use typescriptDefaults for the typescript language and typescript.javascriptDefaults for the javascript language.确保将typescriptDefaults用于typescript语言,将typescript.javascriptDefaults用于javascript语言。 I think they are basically equivalent with Monaco.我认为他们基本上等同于摩纳哥。 This had tripped me up for a while.这让我绊倒了一段时间。

If you are using it in React JS using this library: https://www.npmjs.com/package/@monaco-editor/如果你使用这个库在 React JS 中使用它: https : //www.npmjs.com/package/@monaco-editor/

import Editor from "@monaco-editor/react"从“@monaco-editor/react”导入编辑器

Then you can write down below points to make changes in the Editor's behaviours:然后你可以写下以下几点来改变编辑器的行为:

<Editor
  height="90vh"
  defaultLanguage="javascript"
  defaultValue={code}
  options={{
    quickSuggestions: {
      other: false,
      comments: false,
      strings: false
    },
    parameterHints: {
      enabled: false
    },
    suggestOnTriggerCharacters: false,
    acceptSuggestionOnEnter: "off",
    tabCompletion: "off",
    wordBasedSuggestions: false
  }}
/>

To see the list of possible options please check IStandaloneEditorConstructionOptions要查看可能的选项列表,请检查IStandaloneEditorConstructionOptions

for those that need to disable default libs and provide their libs, you could disable default libs by noLib option对于那些需要禁用默认库并提供其库的用户,您可以通过noLib选项禁用默认库

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  target: monaco.languages.typescript.ScriptTarget.ES2015,
  noLib: true, 
  allowNonTsExtensions: true,
  checkJs: true,
  strict: true,
  allowJs: true,
});

then go here to download lib that you prefer, reduce file and keep what you want then export file content as a const sring然后 go在这里下载你喜欢的库,减少文件并保留你想要的然后将文件内容导出为 const sring

export const es5String = `interface Array<T> {
/**
 * Returns the value of the first element in the array where predicate is true, and undefined
 * otherwise.
 * @param predicate find calls predicate once for each element of the array, in ascending
 * order, until it finds one where predicate returns true. If such an element is found, find
 * immediately returns that element value. Otherwise, find returns undefined.
 * @param thisArg If provided, it will be used as the this value for each invocation of
 * predicate. If it is not provided, undefined is used instead.
 */
find<S extends T>(predicate: (this: void, value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined;
find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined;
.......
.......

finally import that string and use it as custom extra lib, something like this最后导入该字符串并将其用作自定义的额外库,就像这样

import { es5String } from "./es5Custom.core"

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  target: monaco.languages.typescript.ScriptTarget.ES2015,
  noLib: true, 
  allowNonTsExtensions: true,
  checkJs: true,
  strict: true,
  allowJs: true,
});

monaco.languages.typescript.javascriptDefaults.addExtraLib(
  es5String,
  "filename/es5.d.ts"
);

you could add your custom libs also.你也可以添加你的自定义库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Javascript 删除 Monaco Editor 的默认 DOM/浏览器相关自动完成? - How to remove default DOM/Browser related autocompletions for Monaco Editor using Javascript? 如何在摩纳哥编辑器中删除javascript模式的关键字 - How to remove keywords of javascript mode in monaco editor 如何使用简单的 JavaScript 或 jQuery 在浏览器中初始化 Microsoft Monaco 编辑器 - How to initialize Microsoft Monaco editor in a browser using simple JavaScript or jQuery 如何在 Monaco Editor 中添加、删除和更改字形 - How to add, remove and change glyphs in Monaco Editor 如何在 Monaco 编辑器中删除小地图、滚动条以及如何在 React js 中禁用在 Monaco 编辑器中写入? - How to remove minimap, scrollbar in Monaco editor and how to disable writing in Monaco editor in react js? 如何使用量角器在monaco编辑器中插入代码? - How to insert code in monaco editor using the protractor? 如何使用摩纳哥编辑器自动完成 groovy 方法 - How to autocomplete groovy methods using monaco editor 如何在 Monaco-Editor 中禁用内置 javascript/typescript 建议? - How to disable build-in javascript/typescript suggestions in Monaco-Editor? 从Monaco编辑器中删除Listner - Remove Listner from Monaco editor 在 Monaco Editor 上为 JavaScript 设置“this”上下文 - Setting “this” context on Monaco Editor for javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM