XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.VSIX

【VSIX】XFEExtension的Visual Studio扩展

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.VSIX

完成部分代码片段

65e5f16
XFE工作室室长 <xfe18827675401@126.com>
提交于

代码差异

8 个文件 +275 -23
Modified XFEExtension.NetCore.VSIX/XFEExtension.NetCore.VSIX.csproj +13 -18
@@ -48,33 +48,30 @@
48 48 <Compile Include="Properties\AssemblyInfo.cs" />
49 49 <Compile Include="SnippetUtilities.cs" />
50 50 <Compile Include="XFEExtension.NetCore.VSIXPackage.cs" />
51 <Compile Include="XFEExtensionCompletionCommandHandler.cs" />
52 <Compile Include="XFEExtensionCompletionHandlerProvider.cs" />
53 <Compile Include="XFEExtensionCompletionSource.cs" />
54 <Compile Include="XFEExtensionCompletionSourceProvider.cs" />
51 55 </ItemGroup>
52 56 <ItemGroup>
53 <Content Include="CodeSnippets\1033\XFESnippets.xml" />
54 <None Include="CodeSnippets\CSharp\cr.snippet">
57 <Content Include="XFEExtensionSnippets\XFESnippets.xml" />
58 <Content Include="XFEExtensionSnippets\CodeSnippets\cr.snippet">
55 59 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
56 </None>
57 <None Include="CodeSnippets\CSharp\to.snippet">
58 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
59 </None>
60 <IncludeInVSIX>true</IncludeInVSIX>
61 </Content>
62 <Content Include="XFEExtensionSnippets\CodeSnippets\to.snippet">
63 <IncludeInVSIX>true</IncludeInVSIX>
64 </Content>
60 65 <None Include="source.extension.vsixmanifest">
61 66 <SubType>Designer</SubType>
62 67 </None>
63 68 </ItemGroup>
64 69 <ItemGroup>
70 <Reference Include="PresentationCore" />
65 71 <Reference Include="System" />
66 72 </ItemGroup>
67 73 <ItemGroup>
68 74 <PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.10.40171" ExcludeAssets="runtime" />
69 <PackageReference Include="Microsoft.VisualStudio.Shell.Immutable.10.0">
70 <Version>16.10.31321.278</Version>
71 </PackageReference>
72 <PackageReference Include="Microsoft.VisualStudio.TextManager.Interop">
73 <Version>17.10.40170</Version>
74 </PackageReference>
75 <PackageReference Include="Microsoft.VisualStudio.TextManager.Interop.8.0">
76 <Version>17.10.40170</Version>
77 </PackageReference>
78 75 <PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.10.2185">
79 76 <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
80 77 <PrivateAssets>all</PrivateAssets>
@@ -98,9 +95,7 @@
98 95 <IncludeInVSIX>true</IncludeInVSIX>
99 96 </Content>
100 97 </ItemGroup>
101 <ItemGroup>
102 <Folder Include="CodeSnippets\Normal\" />
103 </ItemGroup>
98 <ItemGroup />
104 99 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
105 100 <Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
106 101 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Added XFEExtension.NetCore.VSIX/XFEExtensionCompletionCommandHandler.cs +146 -0
@@ -0,0 +1,146 @@
1 using Microsoft.VisualStudio;
2 using Microsoft.VisualStudio.Language.Intellisense;
3 using Microsoft.VisualStudio.OLE.Interop;
4 using Microsoft.VisualStudio.Shell;
5 using Microsoft.VisualStudio.Text;
6 using Microsoft.VisualStudio.Text.Editor;
7 using Microsoft.VisualStudio.TextManager.Interop;
8 using System;
9 using System.Runtime.InteropServices;
10
11 namespace XFEExtension.NetCore.VSIX
12 {
13 [ProvideLanguageCodeExpansion(
14 SnippetUtilities.LanguageServiceGuidStr,
15 "XFEExtensionSnippets", //the language name
16 0, //the resource id of the language
17 "XFEExtensionSnippets", //the language ID used in the .snippet files
18 @"%InstallRoot%\XFEExtensionSnippets\XFEExtensionSnippets.xml",
19 //the path of the index file
20 SearchPaths = @"%InstallRoot%\XFEExtensionSnippets\CodeSnippets\",
21 ForceCreateDirs = @"%InstallRoot%\XFEExtensionSnippets\CodeSnippets\")]
22 internal class XFEExtensionCompletionCommandHandler: IOleCommandTarget
23 {
24 private IOleCommandTarget m_nextCommandHandler;
25 private ITextView m_textView;
26 private XFEExtensionCompletionHandlerProvider m_provider;
27 private ICompletionSession m_session;
28 internal XFEExtensionCompletionCommandHandler(IVsTextView textViewAdapter, ITextView textView, XFEExtensionCompletionHandlerProvider provider)
29 {
30 this.m_textView = textView;
31 this.m_provider = provider;
32
33 //add the command to the command chain
34 textViewAdapter.AddCommandFilter(this, out m_nextCommandHandler);
35 }
36 public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
37 {
38 if (!VsShellUtilities.IsInAutomationFunction(m_provider.ServiceProvider))
39 {
40 if (pguidCmdGroup == VSConstants.VSStd2K && cCmds > 0)
41 {
42 // make the Insert Snippet command appear on the context menu
43 if ((uint)prgCmds[0].cmdID == (uint)VSConstants.VSStd2KCmdID.INSERTSNIPPET)
44 {
45 prgCmds[0].cmdf = (int)Constants.MSOCMDF_ENABLED | (int)Constants.MSOCMDF_SUPPORTED;
46 return VSConstants.S_OK;
47 }
48 }
49 }
50
51 return m_nextCommandHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
52 }
53
54 public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
55 {
56 if (VsShellUtilities.IsInAutomationFunction(m_provider.ServiceProvider))
57 {
58 return m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
59 }
60 //make a copy of this so we can look at it after forwarding some commands
61 uint commandID = nCmdID;
62 char typedChar = char.MinValue;
63 //make sure the input is a char before getting it
64 if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
65 {
66 typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
67 }
68
69 //check for a commit character
70 if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN
71 || nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB
72 || (char.IsWhiteSpace(typedChar) || char.IsPunctuation(typedChar)))
73 {
74 //check for a selection
75 if (m_session != null && !m_session.IsDismissed)
76 {
77 //if the selection is fully selected, commit the current session
78 if (m_session.SelectedCompletionSet.SelectionStatus.IsSelected)
79 {
80 m_session.Commit();
81 //also, don't add the character to the buffer
82 return VSConstants.S_OK;
83 }
84 else
85 {
86 //if there is no selection, dismiss the session
87 m_session.Dismiss();
88 }
89 }
90 }
91
92 //pass along the command so the char is added to the buffer
93 int retVal = m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
94 bool handled = false;
95 if (!typedChar.Equals(char.MinValue) && char.IsLetterOrDigit(typedChar))
96 {
97 if (m_session == null || m_session.IsDismissed) // If there is no active session, bring up completion
98 {
99 this.TriggerCompletion();
100 m_session.Filter();
101 }
102 else //the completion session is already active, so just filter
103 {
104 m_session.Filter();
105 }
106 handled = true;
107 }
108 else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE //redo the filter if there is a deletion
109 || commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
110 {
111 if (m_session != null && !m_session.IsDismissed)
112 m_session.Filter();
113 handled = true;
114 }
115 if (handled) return VSConstants.S_OK;
116 return retVal;
117 }
118 private bool TriggerCompletion()
119 {
120 //the caret must be in a non-projection location
121 SnapshotPoint? caretPoint =
122 m_textView.Caret.Position.Point.GetPoint(
123 textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);
124 if (!caretPoint.HasValue)
125 {
126 return false;
127 }
128
129 m_session = m_provider.CompletionBroker.CreateCompletionSession
130 (m_textView,
131 caretPoint.Value.Snapshot.CreateTrackingPoint(caretPoint.Value.Position, PointTrackingMode.Positive),
132 true);
133
134 //subscribe to the Dismissed event on the session
135 m_session.Dismissed += this.OnSessionDismissed;
136 m_session.Start();
137
138 return true;
139 }
140 private void OnSessionDismissed(object sender, EventArgs e)
141 {
142 m_session.Dismissed -= this.OnSessionDismissed;
143 m_session = null;
144 }
145 }
146 }
Added XFEExtension.NetCore.VSIX/XFEExtensionCompletionHandlerProvider.cs +34 -0
@@ -0,0 +1,34 @@
1 using Microsoft.VisualStudio.Editor;
2 using Microsoft.VisualStudio.Language.Intellisense;
3 using Microsoft.VisualStudio.Shell;
4 using Microsoft.VisualStudio.Text.Editor;
5 using Microsoft.VisualStudio.TextManager.Interop;
6 using Microsoft.VisualStudio.Utilities;
7 using System;
8 using System.Composition;
9
10 namespace XFEExtension.NetCore.VSIX
11 {
12 [Export(typeof(IVsTextViewCreationListener))]
13 [Name("token completion handler")]
14 [ContentType("plaintext")]
15 [TextViewRole(PredefinedTextViewRoles.Editable)]
16 internal class XFEExtensionCompletionHandlerProvider : IVsTextViewCreationListener
17 {
18 [Import]
19 internal IVsEditorAdaptersFactoryService AdapterService { get; set; } = null;
20 [Import]
21 internal ICompletionBroker CompletionBroker { get; set; }
22 [Import]
23 internal SVsServiceProvider ServiceProvider { get; set; }
24 public void VsTextViewCreated(IVsTextView textViewAdapter)
25 {
26 ITextView textView = AdapterService.GetWpfTextView(textViewAdapter);
27 if (textView == null)
28 return;
29
30 Func<XFEExtensionCompletionCommandHandler> createCommandHandler = delegate () { return new XFEExtensionCompletionCommandHandler(textViewAdapter, textView, this); };
31 textView.Properties.GetOrCreateSingletonProperty(createCommandHandler);
32 }
33 }
34 }
Added XFEExtension.NetCore.VSIX/XFEExtensionCompletionSource.cs +55 -0
@@ -0,0 +1,55 @@
1 using Microsoft.VisualStudio.Language.Intellisense;
2 using Microsoft.VisualStudio.Text;
3 using Microsoft.VisualStudio.Text.Operations;
4 using System;
5 using System.Collections.Generic;
6
7 namespace XFEExtension.NetCore.VSIX
8 {
9 internal class XFEExtensionCompletionSource : ICompletionSource
10 {
11 private XFEExtensionCompletionSourceProvider m_sourceProvider;
12 private ITextBuffer m_textBuffer;
13 private List<Completion> m_compList;
14 public XFEExtensionCompletionSource(XFEExtensionCompletionSourceProvider sourceProvider, ITextBuffer textBuffer)
15 {
16 m_sourceProvider = sourceProvider;
17 m_textBuffer = textBuffer;
18 }
19 void ICompletionSource.AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets)
20 {
21 List<string> strList = new List<string>();
22 strList.Add("addition");
23 strList.Add("adaptation");
24 strList.Add("subtraction");
25 strList.Add("summation");
26 m_compList = new List<Completion>();
27 foreach (string str in strList)
28 m_compList.Add(new Completion(str, str, str, null, null));
29
30 completionSets.Add(new CompletionSet(
31 "Tokens", //the non-localized title of the tab
32 "Tokens", //the display title of the tab
33 FindTokenSpanAtPosition(session.GetTriggerPoint(m_textBuffer),
34 session),
35 m_compList,
36 null));
37 }
38 private ITrackingSpan FindTokenSpanAtPosition(ITrackingPoint point, ICompletionSession session)
39 {
40 SnapshotPoint currentPoint = (session.TextView.Caret.Position.BufferPosition) - 1;
41 ITextStructureNavigator navigator = m_sourceProvider.NavigatorService.GetTextStructureNavigator(m_textBuffer);
42 TextExtent extent = navigator.GetExtentOfWord(currentPoint);
43 return currentPoint.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
44 }
45 private bool m_isDisposed;
46 public void Dispose()
47 {
48 if (!m_isDisposed)
49 {
50 GC.SuppressFinalize(this);
51 m_isDisposed = true;
52 }
53 }
54 }
55 }
Added XFEExtension.NetCore.VSIX/XFEExtensionCompletionSourceProvider.cs +22 -0
@@ -0,0 +1,22 @@
1 using Microsoft.VisualStudio.Language.Intellisense;
2 using Microsoft.VisualStudio.Text;
3 using Microsoft.VisualStudio.Text.Operations;
4 using Microsoft.VisualStudio.Utilities;
5 using System.Composition;
6 using System.Threading.Tasks;
7
8 namespace XFEExtension.NetCore.VSIX
9 {
10 [Export(typeof(ICompletionSourceProvider))]
11 [ContentType("plaintext")]
12 [Name("token completion")]
13 internal class XFEExtensionCompletionSourceProvider : ICompletionSourceProvider
14 {
15 [Import]
16 internal ITextStructureNavigatorSelectorService NavigatorService { get; set; }
17 public ICompletionSource TryCreateCompletionSource(ITextBuffer textBuffer)
18 {
19 return new XFEExtensionCompletionSource(this, textBuffer);
20 }
21 }
22 }
Renamed XFEExtension.NetCore.VSIX/XFEExtensionSnippets/CodeSnippets/cr.snippet +2 -2
@@ -1,5 +1,5 @@
1 1 <?xml version="1.0" encoding="utf-8"?>
2 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
2 <XFEExtensionSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
3 3 <CodeSnippet Format="1.0.0">
4 4 <Header>
5 5 <Title>cr</Title>
@@ -21,4 +21,4 @@
21 21 </Code>
22 22 </Snippet>
23 23 </CodeSnippet>
24 </CodeSnippets>
24 </XFEExtensionSnippets>
Renamed XFEExtension.NetCore.VSIX/XFEExtensionSnippets/CodeSnippets/to.snippet +2 -2
@@ -1,5 +1,5 @@
1 1 <?xml version="1.0" encoding="utf-8"?>
2 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
2 <XFEExtensionSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
3 3 <CodeSnippet Format="1.0.0">
4 4 <Header>
5 5 <Title>to</Title>
@@ -21,4 +21,4 @@
21 21 </Code>
22 22 </Snippet>
23 23 </CodeSnippet>
24 </CodeSnippets>
24 </XFEExtensionSnippets>
Renamed XFEExtension.NetCore.VSIX/XFEExtensionSnippets/XFESnippets.xml +1 -1
@@ -5,7 +5,7 @@
5 5 <OnOff>On</OnOff>
6 6 <Installed>true</Installed>
7 7 <Locale>1033</Locale>
8 <DirPath>%InstallRoot%\CodeSnippets\CSharp\%LCID%\</DirPath>
8 <DirPath>%InstallRoot%\XFEExtensionSnippets\CodeSnippets\</DirPath>
9 9 <LocalizedName>Snippets</LocalizedName>
10 10 </SnippetDir>
11 11 </Language>