You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Monaco Editor悬浮提示中隐藏marker.message与marker.code?

Hiding Marker Message/Code in Monaco Editor's Custom Hover

I'm building a custom IDE using Monaco Editor and leveraging the provideHover function for specific code patterns. I’ve noticed that the hover window displays two sets of content: my custom text from the contents array I return, and additional text pulled from marker.message and marker.code (from the IMarker interface).

Is there a way to hide the auto-generated text that comes from marker.message and marker.code?

Here’s my current provideHover implementation:

public async provideHover(model: monaco.editor.ITextModel, position: monaco.Position, token: CancellationToken): Promise<monaco.languages.Hover | undefined> { 
  let marker = monaco.editor.getModelMarkers({}).find(marker => { 
    let markerStart = new monaco.Position(marker.startLineNumber, marker.startColumn); 
    let markerEnd = new monaco.Position(marker.endLineNumber, marker.endColumn); 
    if (markerStart.isBeforeOrEqual(position) && position.isBeforeOrEqual(markerEnd)) return marker; 
    return null; 
  }); 
  if (marker != null) { 
    if (marker.code == "113") { 
      return { 
        contents: [{ value: "contents[0].value ", isTrusted: false } , { value: "contents[1].value", isTrusted: false } ] 
      }; 
    } 
  } 
  return undefined 
}

Answer

Great question! The root issue here is that Monaco Editor automatically merges hover content from all registered providers—including your custom provideHover implementation and the built-in marker hover provider that surfaces marker.message and marker.code. To hide that auto-generated marker text, you have two reliable approaches depending on your needs:

1. Disable the built-in marker hover provider entirely

If you want full control over all hover content and don’t need the default marker hover anywhere in your IDE, you can override the default behavior by registering a dummy hover provider for your target language that returns undefined. Add this code when setting up your editor:

monaco.languages.registerHoverProvider('your-language-id', {
  provideHover: () => undefined
});

Replace 'your-language-id' with the actual language ID you’re using (e.g., 'javascript' or your custom language identifier). This tells Monaco to skip showing the default marker hover for that language, leaving only your custom contents visible in the hover window.

2. Override marker hover only for specific markers

If you still want default marker hover text to appear for most markers but want to suppress it for specific cases (like marker code "113" in your example), you can create a single hover provider that handles both scenarios:

monaco.languages.registerHoverProvider('your-language-id', {
  async provideHover(model: monaco.editor.ITextModel, position: monaco.Position, token: CancellationToken): Promise<monaco.languages.Hover | undefined> {
    // Find the marker at the current position
    const marker = monaco.editor.getModelMarkers({}).find(marker => {
      const markerStart = new monaco.Position(marker.startLineNumber, marker.startColumn);
      const markerEnd = new monaco.Position(marker.endLineNumber, marker.endColumn);
      return markerStart.isBeforeOrEqual(position) && position.isBeforeOrEqual(markerEnd);
    });

    if (marker) {
      if (marker.code === "113") {
        // Return only your custom content for this marker
        return {
          contents: [
            { value: "contents[0].value ", isTrusted: false },
            { value: "contents[1].value", isTrusted: false }
          ]
        };
      } else {
        // Replicate the default marker hover content for other markers
        const markerContent: string[] = [];
        if (marker.code) markerContent.push(`**Code:** \`${marker.code}\``);
        if (marker.message) markerContent.push(marker.message);
        
        return {
          contents: [{ value: markerContent.join('\n\n'), isTrusted: false }]
        };
      }
    }

    // No marker found? Return undefined to let other providers run
    return undefined;
  }
});

This approach gives you granular control: for marker code "113", you show only your custom text; for all other markers, you replicate the default hover behavior; and for non-marker positions, you allow other hover providers (if any) to do their work.


内容的提问来源于stack exchange,提问作者SoftTimur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:17:35