How to remove the toolbar menu for .sql files

Vasu Bansal 20 Reputation points
2025-06-11T10:07:49.8333333+00:00

User's image

I’m trying to remove the toolbar menu that appears specifically when .sql files are opened in Visual Studio.

Do we need to implement a custom editor factory to achieve this? If yes, could you please explain how that can be done? If not, is there a better or recommended way to suppress the default toolbar?

Also I am using my own language service when i open the .sql file like this.

 Guid langGuid = new Guid("language service id");
 VsShellUtilities.OpenDocument(
     ServiceProvider.GlobalProvider,
     openFileDialog.FileName,
     VSConstants.LOGVIEWID_Primary,
     out IVsUIHierarchy hierarchy,
     out uint itemID,
     out IVsWindowFrame windowFrame
 );
 object docData;
 windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out docData);
 IVsTextBuffer textBuffer = docData as IVsTextBuffer;
 if (textBuffer == null && docData is IVsTextLines lines)
 {
     textBuffer = lines as IVsTextBuffer;
 }
 int result = textBuffer.SetLanguageServiceID(ref langGuid);
 if (result != VSConstants.S_OK)
 {
     // Handle error: language service might not be registered or available
 }
 windowFrame?.Show(); 

using this I am able to use my language service features but for the .sql files this toolbar is coming.

Developer technologies Visual Studio Extensions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Kenuli Bulathsinghela 0 Reputation points
    2025-06-17T08:07:00+00:00

    Why the toolbar shows up:

    You're using your language service, but not your editor.

    What’s missing:

    A custom editor factory to override Visual Studio’s default .sql file behavior.

    How to fix it:

    Create and register a custom editor for .sql, and return Guid.Empty for the toolbar command UI ID.


  2. Kenuli Bulathsinghela 0 Reputation points
    2025-06-17T13:38:23.54+00:00

    Step 01 : Create the Custom Editor Factory

    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using System;
    using System.Runtime.InteropServices;
    [Guid("D1A5D9FA-32F0-4001-BDD2-BB3D50F4E6C3")] // Use a unique GUID
    public class SqlEditorFactory : IVsEditorFactory
    {
        private ServiceProvider _serviceProvider;
        public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
        {
            _serviceProvider = new ServiceProvider(psp);
            return VSConstants.S_OK;
        }
        public int Close() => VSConstants.S_OK;
        public int CreateEditorInstance(
            uint grfCreateDoc,
            string pszMkDocument,
            string pszPhysicalView,
            IVsHierarchy pvHier,
            uint itemid,
            IntPtr punkDocDataExisting,
            out IntPtr ppunkDocView,
            out IntPtr ppunkDocData,
            out string pbstrEditorCaption,
            out Guid pguidCmdUI,
            out int pgrfCDW)
        {
            ppunkDocView = IntPtr.Zero;
            ppunkDocData = IntPtr.Zero;
            pbstrEditorCaption = string.Empty;
            pgrfCDW = 0;
            // Use a simple text buffer
            IVsTextLines textLines = new VsTextBufferClass();
            // Set the docData and docView to the same buffer (text editor)
            ppunkDocData = Marshal.GetIUnknownForObject(textLines);
            ppunkDocView = ppunkDocData;
            // 🚫 No toolbar
            pguidCmdUI = Guid.Empty;
            return VSConstants.S_OK;
        }
    }
    

    Step 2: Register the Editor in Your Package

    In your AsyncPackage-derived class (e.g., MyPackage.cs), add these attributes at the top of the class:

    [ProvideEditorFactory(typeof(SqlEditorFactory), 101)]
    [ProvideEditorExtension(typeof(SqlEditorFactory), ".sql", 50,
        NameResourceID = 101,
        DefaultName = "My SQL Editor")]
    [ProvideEditorLogicalView(typeof(SqlEditorFactory), VSConstants.LOGVIEWID_Primary_string)]
    

    And in the InitializeAsync() method of the package:

    protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
    {
        await base.InitializeAsync(cancellationToken, progress);
        // Register the custom editor
        RegisterEditorFactory(new SqlEditorFactory());
    }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.