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

请教下有没有办法配合 monaco-editor-nginx 一起使用 #39

Closed
devhaozi opened this issue Nov 4, 2023 · 4 comments
Closed

请教下有没有办法配合 monaco-editor-nginx 一起使用 #39

devhaozi opened this issue Nov 4, 2023 · 4 comments

Comments

@devhaozi
Copy link

devhaozi commented Nov 4, 2023

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

想用它给编辑器添加 nginx 格式支持。

@devhaozi devhaozi changed the title 请教下有没有办法配合 nginx-editor 一起使用 请教下有没有办法配合 monaco-editor-nginx 一起使用 Nov 4, 2023
@imguolao
Copy link
Owner

imguolao commented Nov 6, 2023

你可以在 onBeforeMount 里面拿到 monaco 实例,然后像你发的这个仓库里的代码一样,注册 nginx 为新语言,设置提示补全和高亮。

https://github.com/jaywcjlove/nginx-editor/blob/c0a4b62ad0ad401f703e8ac3b711d9da9e700cec/src/index.tsx#L7-L60

@devhaozi
Copy link
Author

devhaozi commented Nov 6, 2023

你可以在 onBeforeMount 里面拿到 monaco 实例,然后像你发的这个仓库里的代码一样,注册 nginx 为新语言,设置提示补全和高亮。

https://github.com/jaywcjlove/nginx-editor/blob/c0a4b62ad0ad401f703e8ac3b711d9da9e700cec/src/index.tsx#L7-L60

搞定了,谢谢!

@Nobilta
Copy link

Nobilta commented Apr 22, 2024

你可以在 onBeforeMount 里面拿到 monaco 实例,然后像你发的这个仓库里的代码一样,注册 nginx 为新语言,设置提示补全和高亮。
https://github.com/jaywcjlove/nginx-editor/blob/c0a4b62ad0ad401f703e8ac3b711d9da9e700cec/src/index.tsx#L7-L60

搞定了,谢谢!

想请教下具体是如何实现的呢?

beforeMount() {
    const monaco = this.$options.components.VueMonacoEditor;
    monaco.languages.register({
      id: 'nginx',
    });
    }

我尝试这样注册后会提示找不到register方法,好像还是没找到monaco实例

@devhaozi
Copy link
Author

你可以在 onBeforeMount 里面拿到 monaco 实例,然后像你发的这个仓库里的代码一样,注册 nginx 为新语言,设置提示补全和高亮。
https://github.com/jaywcjlove/nginx-editor/blob/c0a4b62ad0ad401f703e8ac3b711d9da9e700cec/src/index.tsx#L7-L60

搞定了,谢谢!

想请教下具体是如何实现的呢?

beforeMount() {
    const monaco = this.$options.components.VueMonacoEditor;
    monaco.languages.register({
      id: 'nginx',
    });
    }

我尝试这样注册后会提示找不到register方法,好像还是没找到monaco实例

const editorOnBeforeMount = (monaco: any) => {
  monaco.languages.register({
    id: 'nginx'
  })

  monaco.languages.setMonarchTokensProvider('nginx', tokenConf)
  monaco.editor.defineTheme('nginx-theme', themeConfig)
  monaco.editor.defineTheme('nginx-theme-dark', themeDarkConfig)

  monaco.languages.registerCompletionItemProvider('nginx', {
    provideCompletionItems: (model: any, position: any) => {
      const word = model.getWordUntilPosition(position)
      const range = {
        startLineNumber: position.lineNumber,
        endLineNumber: position.lineNumber,
        startColumn: word.startColumn,
        endColumn: word.endColumn
      }
      return { suggestions: suggestions(range) }
    }
  })

  monaco.languages.registerHoverProvider('nginx', {
    provideHover: (model: any, position: any) => {
      const word = model.getWordAtPosition(position)
      if (!word) return
      const data = directives.find((item) => item.n === word.word || item.n === `$${word.word}`)
      if (!data) return
      const range = {
        startLineNumber: position.lineNumber,
        endLineNumber: position.lineNumber,
        startColumn: word.startColumn,
        endColumn: word.endColumn
      }
      const contents = [{ value: `**\`${data.n}\`** | ${data.m} | ${data.c || ''}` }]
      if (data.s) {
        contents.push({ value: `**syntax:** ${data.s || ''}` })
      }
      if (data.v) {
        contents.push({ value: `**default:** ${data.v || ''}` })
      }
      if (data.d) {
        contents.push({ value: `${data.d}` })
      }
      return {
        contents: [...contents],
        range: range
      }
    }
  })
}

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

No branches or pull requests

3 participants