From 7eec395a6da6d8238c99ef4c856cf137818171d1 Mon Sep 17 00:00:00 2001 From: Mads Kristensen Date: Sun, 9 Jan 2022 11:05:14 -0800 Subject: [PATCH] Fixed editor factory registration --- src/Editor/EditorFeatures.cs | 2 + src/Editor/IntelliSense.cs | 282 +++++++++++++++++----------------- src/Editor/LanguageFactory.cs | 2 + src/PkgdefLanguage.csproj | 4 +- src/PkgdefPackage.cs | 15 +- src/VSCommandTable.cs | 4 +- src/VSCommandTable.vsct | 9 +- 7 files changed, 164 insertions(+), 154 deletions(-) diff --git a/src/Editor/EditorFeatures.cs b/src/Editor/EditorFeatures.cs index b43a718..6e07865 100644 --- a/src/Editor/EditorFeatures.cs +++ b/src/Editor/EditorFeatures.cs @@ -3,7 +3,9 @@ using Microsoft.VisualStudio.Language.Intellisense; using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; using Microsoft.VisualStudio.Language.StandardClassification; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Text.BraceCompletion; +using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Tagging; using Microsoft.VisualStudio.Utilities; diff --git a/src/Editor/IntelliSense.cs b/src/Editor/IntelliSense.cs index 8671cf4..73d67c1 100644 --- a/src/Editor/IntelliSense.cs +++ b/src/Editor/IntelliSense.cs @@ -1,141 +1,141 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.ComponentModel.Composition; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.VisualStudio.Core.Imaging; -using Microsoft.VisualStudio.Imaging; -using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; -using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data; -using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Shell.Interop; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Adornments; -using Microsoft.VisualStudio.Text.Editor; -using Microsoft.VisualStudio.Utilities; -using Microsoft.Win32; - -namespace PkgdefLanguage -{ - [Export(typeof(IAsyncCompletionSourceProvider))] - [ContentType(Constants.LanguageName)] - [Name(Constants.LanguageName)] - public class IntelliSense : IAsyncCompletionSourceProvider - { - public IAsyncCompletionSource GetOrCreate(ITextView textView) => - textView.Properties.GetOrCreateSingletonProperty(() => new AsyncCompletionSource()); - } - - public class AsyncCompletionSource : IAsyncCompletionSource - { - private static readonly ImageElement _referenceIcon = new(KnownMonikers.LocalVariable.ToImageId(), "Variable"); - - public Task GetCompletionContextAsync(IAsyncCompletionSession session, CompletionTrigger trigger, SnapshotPoint triggerLocation, SnapshotSpan applicableToSpan, CancellationToken cancellationToken) - { - Document document = session.TextView.TextBuffer.GetDocument(); - ParseItem item = document.FindItemFromPosition(triggerLocation.Position); - IEnumerable items = null; - - if (item?.Type == ItemType.Reference) - { - items = GetReferenceCompletion(); - } - else if (item?.Type == ItemType.RegistryKey) - { - items = GetRegistryKeyCompletion(item, triggerLocation); - } - - return Task.FromResult(items == null ? null : new CompletionContext(items.ToImmutableArray())); - } - - private IEnumerable GetRegistryKeyCompletion(ParseItem item, SnapshotPoint triggerLocation) - { - ITextSnapshotLine line = triggerLocation.GetContainingLine(); - var column = triggerLocation.Position - line.Start - 1; - var previousKey = item.Text.LastIndexOf('\\', column); - - if (previousKey > -1) - { - IEnumerable prevKeys = item.Text.Substring(0, previousKey).Split('\\').Skip(1); - RegistryKey root = VSRegistry.RegistryRoot(__VsLocalRegistryType.RegType_Configuration); - RegistryKey parent = root; - - foreach (var subKey in prevKeys) - { - parent = parent.OpenSubKey(subKey); - } - - return parent?.GetSubKeyNames()?.Select(s => new CompletionItem(s, this, _referenceIcon)); - } - - return null; - } - - private IEnumerable GetReferenceCompletion() - { - foreach (var key in PredefinedVariables.Variables.Keys) - { - var completion = new CompletionItem(key, this, _referenceIcon, ImmutableArray.Empty, "", $"${key}$", key, key, ImmutableArray.Empty); - completion.Properties.AddProperty("description", PredefinedVariables.Variables[key]); - yield return completion; - } - } - - public Task GetDescriptionAsync(IAsyncCompletionSession session, CompletionItem item, CancellationToken token) - { - if (item.Properties.TryGetProperty("description", out string description)) - { - return Task.FromResult(description); - } - - return Task.FromResult(null); - } - - public CompletionStartData InitializeCompletion(CompletionTrigger trigger, SnapshotPoint triggerLocation, CancellationToken token) - { - if (trigger.Character == '\n' || trigger.Character == ']' || trigger.Reason == CompletionTriggerReason.Deletion) - { - return CompletionStartData.DoesNotParticipateInCompletion; - } - - Document document = triggerLocation.Snapshot.TextBuffer.GetDocument(); - ParseItem item = document?.FindItemFromPosition(triggerLocation.Position); - - if (item?.Type == ItemType.Reference) - { - var tokenSpan = new SnapshotSpan(triggerLocation.Snapshot, item); - return new CompletionStartData(CompletionParticipation.ProvidesItems, tokenSpan); - } - else if (item?.Type == ItemType.RegistryKey && item.Text.IndexOf("$rootkey$", StringComparison.OrdinalIgnoreCase) > -1) - { - var column = triggerLocation.Position - item.Span.Start; - - if (column < 1) - { - return CompletionStartData.DoesNotParticipateInCompletion; ; - } - - var start = item.Text.LastIndexOf('\\', column - 1) + 1; - var end = item.Text.IndexOf('\\', column); - var close = item.Text.IndexOf(']', column); - var textEnd = item.Text.IndexOf(']', column); - end = end >= start ? end : close; - end = end >= start ? end : textEnd; - end = end >= start ? end : item.Text.TrimEnd().Length; - - if (end >= start) - { - if (close == -1 || column <= close) - { - var span = new SnapshotSpan(triggerLocation.Snapshot, item.Span.Start + start, end - start); - return new CompletionStartData(CompletionParticipation.ProvidesItems, span); - } - } - } - - return CompletionStartData.DoesNotParticipateInCompletion; - } - } -} \ No newline at end of file +//using System; +//using System.Collections.Generic; +//using System.Collections.Immutable; +//using System.ComponentModel.Composition; +//using System.Linq; +//using System.Threading; +//using System.Threading.Tasks; +//using Microsoft.VisualStudio.Core.Imaging; +//using Microsoft.VisualStudio.Imaging; +//using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; +//using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data; +//using Microsoft.VisualStudio.Shell; +//using Microsoft.VisualStudio.Shell.Interop; +//using Microsoft.VisualStudio.Text; +//using Microsoft.VisualStudio.Text.Adornments; +//using Microsoft.VisualStudio.Text.Editor; +//using Microsoft.VisualStudio.Utilities; +//using Microsoft.Win32; + +//namespace PkgdefLanguage +//{ +// [Export(typeof(IAsyncCompletionSourceProvider))] +// [ContentType(Constants.LanguageName)] +// [Name(Constants.LanguageName)] +// public class IntelliSense : IAsyncCompletionSourceProvider +// { +// public IAsyncCompletionSource GetOrCreate(ITextView textView) => +// textView.Properties.GetOrCreateSingletonProperty(() => new AsyncCompletionSource()); +// } + +// public class AsyncCompletionSource : IAsyncCompletionSource +// { +// private static readonly ImageElement _referenceIcon = new(KnownMonikers.LocalVariable.ToImageId(), "Variable"); + +// public Task GetCompletionContextAsync(IAsyncCompletionSession session, CompletionTrigger trigger, SnapshotPoint triggerLocation, SnapshotSpan applicableToSpan, CancellationToken cancellationToken) +// { +// Document document = session.TextView.TextBuffer.GetDocument(); +// ParseItem item = document.FindItemFromPosition(triggerLocation.Position); +// IEnumerable items = null; + +// if (item?.Type == ItemType.Reference) +// { +// items = GetReferenceCompletion(); +// } +// else if (item?.Type == ItemType.RegistryKey) +// { +// items = GetRegistryKeyCompletion(item, triggerLocation); +// } + +// return Task.FromResult(items == null ? null : new CompletionContext(items.ToImmutableArray())); +// } + +// private IEnumerable GetRegistryKeyCompletion(ParseItem item, SnapshotPoint triggerLocation) +// { +// ITextSnapshotLine line = triggerLocation.GetContainingLine(); +// var column = triggerLocation.Position - line.Start - 1; +// var previousKey = item.Text.LastIndexOf('\\', column); + +// if (previousKey > -1) +// { +// IEnumerable prevKeys = item.Text.Substring(0, previousKey).Split('\\').Skip(1); +// RegistryKey root = VSRegistry.RegistryRoot(__VsLocalRegistryType.RegType_Configuration); +// RegistryKey parent = root; + +// foreach (var subKey in prevKeys) +// { +// parent = parent.OpenSubKey(subKey); +// } + +// return parent?.GetSubKeyNames()?.Select(s => new CompletionItem(s, this, _referenceIcon)); +// } + +// return null; +// } + +// private IEnumerable GetReferenceCompletion() +// { +// foreach (var key in PredefinedVariables.Variables.Keys) +// { +// var completion = new CompletionItem(key, this, _referenceIcon, ImmutableArray.Empty, "", $"${key}$", key, key, ImmutableArray.Empty); +// completion.Properties.AddProperty("description", PredefinedVariables.Variables[key]); +// yield return completion; +// } +// } + +// public Task GetDescriptionAsync(IAsyncCompletionSession session, CompletionItem item, CancellationToken token) +// { +// if (item.Properties.TryGetProperty("description", out string description)) +// { +// return Task.FromResult(description); +// } + +// return Task.FromResult(null); +// } + +// public CompletionStartData InitializeCompletion(CompletionTrigger trigger, SnapshotPoint triggerLocation, CancellationToken token) +// { +// if (trigger.Character == '\n' || trigger.Character == ']' || trigger.Reason == CompletionTriggerReason.Deletion) +// { +// return CompletionStartData.DoesNotParticipateInCompletion; +// } + +// Document document = triggerLocation.Snapshot.TextBuffer.GetDocument(); +// ParseItem item = document?.FindItemFromPosition(triggerLocation.Position); + +// if (item?.Type == ItemType.Reference) +// { +// var tokenSpan = new SnapshotSpan(triggerLocation.Snapshot, item); +// return new CompletionStartData(CompletionParticipation.ProvidesItems, tokenSpan); +// } +// else if (item?.Type == ItemType.RegistryKey && item.Text.IndexOf("$rootkey$", StringComparison.OrdinalIgnoreCase) > -1) +// { +// var column = triggerLocation.Position - item.Span.Start; + +// if (column < 1) +// { +// return CompletionStartData.DoesNotParticipateInCompletion; ; +// } + +// var start = item.Text.LastIndexOf('\\', column - 1) + 1; +// var end = item.Text.IndexOf('\\', column); +// var close = item.Text.IndexOf(']', column); +// var textEnd = item.Text.IndexOf(']', column); +// end = end >= start ? end : close; +// end = end >= start ? end : textEnd; +// end = end >= start ? end : item.Text.TrimEnd().Length; + +// if (end >= start) +// { +// if (close == -1 || column <= close) +// { +// var span = new SnapshotSpan(triggerLocation.Snapshot, item.Span.Start + start, end - start); +// return new CompletionStartData(CompletionParticipation.ProvidesItems, span); +// } +// } +// } + +// return CompletionStartData.DoesNotParticipateInCompletion; +// } +// } +//} \ No newline at end of file diff --git a/src/Editor/LanguageFactory.cs b/src/Editor/LanguageFactory.cs index b2ba223..44d454b 100644 --- a/src/Editor/LanguageFactory.cs +++ b/src/Editor/LanguageFactory.cs @@ -5,6 +5,7 @@ namespace PkgdefLanguage { + [ComVisible(true)] [Guid(PackageGuids.EditorFactoryString)] internal sealed class LanguageFactory : LanguageBase { @@ -34,6 +35,7 @@ public override void SetDefaultPreferences(LanguagePreferences preferences) preferences.IndentSize = 2; preferences.IndentStyle = IndentingStyle.Smart; preferences.ShowNavigationBar = true; + preferences.EnableFormatSelection = true; preferences.WordWrap = true; preferences.WordWrapGlyphs = true; diff --git a/src/PkgdefLanguage.csproj b/src/PkgdefLanguage.csproj index 19b8b3f..0a7f5cf 100644 --- a/src/PkgdefLanguage.csproj +++ b/src/PkgdefLanguage.csproj @@ -26,9 +26,9 @@ true true true - true + false true - false + true Program $(DevEnvDir)devenv.exe /rootsuffix Exp diff --git a/src/PkgdefPackage.cs b/src/PkgdefPackage.cs index be4e947..d1ecc2b 100644 --- a/src/PkgdefPackage.cs +++ b/src/PkgdefPackage.cs @@ -15,24 +15,25 @@ namespace PkgdefLanguage [Guid(PackageGuids.PkgdefLanguageString)] [ProvideMenuResource("Menus.ctmenu", 1)] - [ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, DefaultToInsertSpaces = true, EnableLineNumbers = true, EnableAsyncCompletion = true, EnableCommenting = true, ShowCompletion = true, ShowDropDownOptions = true)] + [ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, EnableLineNumbers = true, EnableAsyncCompletion = true, ShowCompletion = true, EnableFormatSelection = true, ShowDropDownOptions = true)] [ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgDefExt)] [ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgUndefExt)] - [ProvideEditorExtension(typeof(LanguageFactory), Constants.PkgDefExt, 0x32)] - [ProvideEditorExtension(typeof(LanguageFactory), Constants.PkgUndefExt, 0x32)] - [ProvideEditorFactory(typeof(LanguageFactory), 0, CommonPhysicalViewAttributes = (int)__VSPHYSICALVIEWATTRIBUTES.PVA_SupportsPreview, TrustLevel = __VSEDITORTRUSTLEVEL.ETL_AlwaysTrusted)] + + [ProvideEditorFactory(typeof(LanguageFactory), 738, CommonPhysicalViewAttributes = (int)__VSPHYSICALVIEWATTRIBUTES.PVA_SupportsPreview, TrustLevel = __VSEDITORTRUSTLEVEL.ETL_AlwaysTrusted)] + [ProvideEditorExtension(typeof(LanguageFactory), Constants.PkgDefExt, 65535, NameResourceID = 738)] + [ProvideEditorExtension(typeof(LanguageFactory), Constants.PkgUndefExt, 65535, NameResourceID = 738)] [ProvideEditorLogicalView(typeof(LanguageFactory), VSConstants.LOGVIEWID.TextView_string, IsTrusted = true)] - [ProvideEditorLogicalView(typeof(LanguageFactory), VSConstants.LOGVIEWID.Code_string, IsTrusted = true)] + [ProvideFileIcon(Constants.PkgDefExt, "KnownMonikers.RegistrationScript")] [ProvideFileIcon(Constants.PkgUndefExt, "KnownMonikers.RegistrationScript")] public sealed class PkgdefPackage : ToolkitPackage { - protected override Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) + protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) { + await JoinableTaskFactory.SwitchToMainThreadAsync(); var language = new LanguageFactory(this); RegisterEditorFactory(language); ((IServiceContainer)this).AddService(typeof(LanguageFactory), language, true); - return Task.CompletedTask; } } } diff --git a/src/VSCommandTable.cs b/src/VSCommandTable.cs index 2db63b5..78c1915 100644 --- a/src/VSCommandTable.cs +++ b/src/VSCommandTable.cs @@ -12,10 +12,10 @@ namespace PkgdefLanguage /// internal sealed partial class PackageGuids { - public const string EditorFactoryString = "57522df2-e926-4908-8c62-d57afece26e5"; + public const string EditorFactoryString = "57522df2-e926-4908-8c62-d57afece26e4"; public static Guid EditorFactory = new Guid(EditorFactoryString); - public const string PkgdefLanguageString = "4ecd0189-a643-49a1-9f2a-6f32e299dd7d"; + public const string PkgdefLanguageString = "4ecd0189-a643-49a1-9f2a-6f32e299dd7c"; public static Guid PkgdefLanguage = new Guid(PkgdefLanguageString); } /// diff --git a/src/VSCommandTable.vsct b/src/VSCommandTable.vsct index 7981155..332560c 100644 --- a/src/VSCommandTable.vsct +++ b/src/VSCommandTable.vsct @@ -10,8 +10,13 @@ + + + + + - - + +