CodeSnippetProvider's Overriden etPossibleShortcutSnapshotRange() not always called

SyntaxEditor for WPF Forum

The latest build of this product (v25.1.3) was released 8 days ago, which was before this thread was created.
Posted 6 days ago by Rick - Developer, Visual Software Systems LLC
Version: 25.1.2
Avatar

I'm implementing Code Snippets in my application.  I've extended CodeSnippetProvider and overriden the GetPossibleShortcutSnapshotRange() method so that snippets won't be inserted within comments and literals.  This works perfectly when typing a snippet key and then pressing Tab within the editor.  However, when I open a code snippet selection session via code using editor.ActiveView.IntelliPrompt.RequestInsertSnippetSession(), select a snippet and enter it by pressing the Tab key, Enter Key or double-clicking the entry, the overriden GetPossibleShortcutSnapshotRange() is not executed so the snippet is inserted when it shouldn't be.  Is there something additional I need to do to get the overriden GetPossibleShortcutSnapshotRange() to execute from programmatically opened code snippet selection session?

You can recreate this in the sample browser's Code Snippet sample.  Add this class to folder \Actipro Software\WPF-Controls\v25.1.2\SampleBrowser\ProductSamples\SyntaxEditorSamples\QuickStart\IntelliPromptCodeSnippets as CodeSnippetProviderEx.cs.  Then update MainControl.xaml.cs in the same folder and modify line 30 to instantiate CodeSnippetProviderEx() rather than CodeSnippetProvider().

using ActiproSoftware.Text;
using ActiproSoftware.Text.Lexing;
using ActiproSoftware.Windows.Controls.SyntaxEditor.IntelliPrompt.Implementation;
using System;
using System.Linq;

namespace ActiproSoftware.ProductSamples.SyntaxEditorSamples.QuickStart.IntelliPromptCodeSnippets {
    internal class CodeSnippetProviderEx : CodeSnippetProvider {
        private string[] TokenKeys = { "MultiLineCommentText", "SingleLineCommentText" };
        protected override TextSnapshotRange GetPossibleShortcutSnapshotRange(TextSnapshotOffset snapshotOffset) {
            TextSnapshotRange SnapshotRange = base.GetPossibleShortcutSnapshotRange(snapshotOffset);

            if (SnapshotRange.Length > 0) {
                ITextSnapshotReader reader = SnapshotRange.Snapshot.GetReader(SnapshotRange.StartOffset);
                IToken token = reader.ReadToken();

                if (TokenKeys.Contains(token.Key))
                    return TextSnapshotRange.Deleted;
            }

            return SnapshotRange;
        }
    }
}

Thanks.

Comments (2)

Posted 4 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Rick,

The GetPossibleShortcutSnapshotRange method is only called after the Tab key is pressed to check if a shortcut was typed.  In your case illustrated here, you're showing a code snippet selection session and when you pick a code snippet to insert, it will not call GetPossibleShortcutSnapshotRange since that is meant only for validating typed shortcuts.

What you'd want to do instead is not even show a code snippet selection session if you are in a comment.  In that sample, you'd want to do your check in OnInsertSnippetButtonClick to determine if RequestInsertSnippetSession should be called or not based on current context.


Actipro Software Support

Posted 4 days ago by Rick - Developer, Visual Software Systems LLC
Avatar

Hi. Thanks for the clarification on when the GetPossibleShortcutSnapshotRange method is called. I'll add a check to to the button's click method as you suggest.

Add Comment

Please log in to a validated account to post comments.