Skip to content

Commit b4cc6b0

Browse files
committed
完成基本的ICompletionSource逻辑; 调整部分代码命名与文件结构;
1 parent 27d647e commit b4cc6b0

16 files changed

+760
-70
lines changed

src/ChinesePinyinIntelliSenseExtender.csproj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5050
</PropertyGroup>
5151
<ItemGroup>
52+
<Compile Include="Intellisense\IIdeographCompletionSet.cs" />
53+
<Compile Include="Intellisense\SyncCompletion\IdeographCompletionSets.cs" />
54+
<Compile Include="Intellisense\SyncCompletion\IdeographCompletions.cs" />
55+
<Compile Include="Intellisense\IIdeographCompletion.cs" />
56+
<Compile Include="Intellisense\SyncCompletion\IdeographCompletionSource.cs" />
57+
<Compile Include="Intellisense\SyncCompletion\IdeographCompletionSourceProvider.cs" />
58+
<Compile Include="Intellisense\SyncCompletion\IdeographCompletionCommandHandler.cs" />
59+
<Compile Include="Intellisense\SyncCompletion\IdeographCompletionHandlerProvider.cs" />
60+
<Compile Include="Internal\ArrayBaseEnumerator.cs" />
5261
<Compile Include="Options\DictionaryCombinationCreateForm.cs">
5362
<SubType>Form</SubType>
5463
</Compile>
@@ -70,8 +79,8 @@
7079
</Compile>
7180
<Compile Include="Options\OptionPages.cs" />
7281
<Compile Include="Options\Model\GeneralOptions.cs" />
73-
<Compile Include="Completion\CompletionSourceBase.cs" />
74-
<Compile Include="Completion\CompletionSourceProviderBase.cs" />
82+
<Compile Include="Intellisense\CompletionSourceBase.cs" />
83+
<Compile Include="Intellisense\CompletionSourceProviderBase.cs" />
7584
<Compile Include="Ref\InputMethodDictionary\InputMethodDictionaryUtilities.cs" />
7685
<Compile Include="Ref\InputMethodDictionary\InputMethodReverseDictionary.cs" />
7786
<Compile Include="Ref\InputMethodDictionary\ITextAdjuster.cs" />
@@ -100,8 +109,8 @@
100109
<Compile Include="Usings.cs" />
101110
<Compile Include="Util\StringPreMatchUtil.cs" />
102111
<Compile Include="Util\InputMethodDictionaryLoader.cs" />
103-
<Compile Include="Completion\Async\PinyinAsyncCompletionSource.cs" />
104-
<Compile Include="Completion\Async\PinyinAsyncCompletionSourceProvider.cs" />
112+
<Compile Include="Intellisense\AsyncCompletion\IdeographAsyncCompletionSource.cs" />
113+
<Compile Include="Intellisense\AsyncCompletion\IdeographAsyncCompletionSourceProvider.cs" />
105114
<Compile Include="Properties\AssemblyInfo.cs" />
106115
<Compile Include="Util\ChineseCheckUtil.cs" />
107116
<Compile Include="ObjectBoolean.cs" />
@@ -126,6 +135,7 @@
126135
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
127136
<IncludeInVSIX>true</IncludeInVSIX>
128137
</Content>
138+
<None Include="Intellisense\SyncCompletion\README.md" />
129139
<None Include="source.extension.vsixmanifest">
130140
<SubType>Designer</SubType>
131141
</None>

src/Completion/Async/PinyinAsyncCompletionSource.cs renamed to src/Intellisense/AsyncCompletion/IdeographAsyncCompletionSource.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data;
1515
using Microsoft.VisualStudio.Text;
1616

17-
namespace ChinesePinyinIntelliSenseExtender.Completion.Async;
17+
namespace ChinesePinyinIntelliSenseExtender.Intellisense.AsyncCompletion;
1818

19-
internal class PinyinAsyncCompletionSource : CompletionSourceBase, IAsyncCompletionSource
19+
internal class IdeographAsyncCompletionSource : CompletionSourceBase, IAsyncCompletionSource
2020
{
2121
#region Private 字段
2222

23-
private static readonly CompletionFilter s_chineseFilter = new("中文代码", "C", new(KnownMonikers.Attribute.ToImageId(), "中文代码"));
23+
private static readonly CompletionFilter s_chineseFilter = new("表意文字代码", "C", new(KnownMonikers.Attribute.ToImageId(), "表意文字代码"));
2424

2525
private static readonly ImmutableArray<CompletionFilter> s_chineseFilters = ImmutableArray.Create(s_chineseFilter);
2626

@@ -30,7 +30,7 @@ internal class PinyinAsyncCompletionSource : CompletionSourceBase, IAsyncComplet
3030

3131
#region Public 构造函数
3232

33-
public PinyinAsyncCompletionSource(IEnumerable<IAsyncCompletionSource> otherAsyncCompletionSources, GeneralOptions options)
33+
public IdeographAsyncCompletionSource(IEnumerable<IAsyncCompletionSource> otherAsyncCompletionSources, GeneralOptions options)
3434
: base(options)
3535
{
3636
_otherAsyncCompletionSources = otherAsyncCompletionSources ?? throw new ArgumentNullException(nameof(otherAsyncCompletionSources));
@@ -42,7 +42,8 @@ public PinyinAsyncCompletionSource(IEnumerable<IAsyncCompletionSource> otherAsyn
4242

4343
public async Task<CompletionContext?> GetCompletionContextAsync(IAsyncCompletionSession session, CompletionTrigger trigger, SnapshotPoint triggerLocation, SnapshotSpan applicableToSpan, CancellationToken token)
4444
{
45-
if (CanNotProcess())
45+
if (s_completionContextRecursionTag.Value
46+
|| !Options.Enable)
4647
{
4748
return null;
4849
}
@@ -59,7 +60,7 @@ public PinyinAsyncCompletionSource(IEnumerable<IAsyncCompletionSource> otherAsyn
5960

6061
token.ThrowIfCancellationRequested();
6162

62-
Func<string, bool> shouldProcessCheckDelegate = StringPreMatchUtil.GetPreCheckPredicate(Options.PreMatchType, Options.PreCheckRule);
63+
var shouldProcessCheckDelegate = StringPreMatchUtil.GetPreCheckPredicate(Options.PreMatchType, Options.PreCheckRule);
6364

6465
var allCompletionItems = tasks.SelectMany(static m => m.Status == TaskStatus.RanToCompletion && m.Result?.Items is not null ? m.Result.Items.AsEnumerable() : Array.Empty<CompletionItem>());
6566

src/Completion/Async/PinyinAsyncCompletionSourceProvider.cs renamed to src/Intellisense/AsyncCompletion/IdeographAsyncCompletionSourceProvider.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
using System.Diagnostics;
55
using System.Threading;
66

7-
using ChinesePinyinIntelliSenseExtender.Options;
8-
97
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
108
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data;
119
using Microsoft.VisualStudio.Text;
1210
using Microsoft.VisualStudio.Text.Editor;
1311
using Microsoft.VisualStudio.Utilities;
1412

15-
namespace ChinesePinyinIntelliSenseExtender.Completion.Async;
13+
namespace ChinesePinyinIntelliSenseExtender.Intellisense.AsyncCompletion;
1614

1715
[Export(typeof(IAsyncCompletionSourceProvider))]
18-
[Name("中文代码拼音补全")]
16+
[Name("表意文字表音补全")]
1917
[ContentType("text")]
20-
internal class PinyinAsyncCompletionSourceProvider : CompletionSourceProviderBase<ITextView, IAsyncCompletionSource>, IAsyncCompletionSourceProvider
18+
internal class IdeographAsyncCompletionSourceProvider : CompletionSourceProviderBase<ITextView, IAsyncCompletionSource>, IAsyncCompletionSourceProvider
2119
{
2220
#region Private 字段
2321

@@ -28,6 +26,15 @@ internal class PinyinAsyncCompletionSourceProvider : CompletionSourceProviderBas
2826

2927
#region Public 方法
3028

29+
public IAsyncCompletionSource GetOrCreate(ITextView textView)
30+
{
31+
return GetOrCreateCompletionSource(textView);
32+
}
33+
34+
#endregion Public 方法
35+
36+
#region Protected 方法
37+
3138
protected override IAsyncCompletionSource CreateCompletionSource(ITextView dependence)
3239
{
3340
var currentContentType = dependence.TextBuffer.ContentType;
@@ -50,7 +57,7 @@ protected override IAsyncCompletionSource CreateCompletionSource(ITextView depen
5057

5158
IAsyncCompletionSource completionSource = otherAsyncCompletionSources is null || otherAsyncCompletionSources.Count == 0
5259
? EmptyAsyncCompletionSource.Instance
53-
: new PinyinAsyncCompletionSource(otherAsyncCompletionSources!, Options);
60+
: new IdeographAsyncCompletionSource(otherAsyncCompletionSources!, Options);
5461

5562
return completionSource;
5663
}
@@ -69,11 +76,11 @@ protected override IAsyncCompletionSource GetDefaultCompletionSource(ITextView d
6976
return EmptyAsyncCompletionSource.Instance;
7077
}
7178

72-
#endregion Public 方法
79+
#endregion Protected 方法
7380

7481
#region Private 类
7582

76-
private class EmptyAsyncCompletionSource : IAsyncCompletionSource
83+
private sealed class EmptyAsyncCompletionSource : IAsyncCompletionSource
7784
{
7885
#region Public 属性
7986

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using ChinesePinyinIntelliSenseExtender.Util;
77
using Microsoft.VisualStudio.Utilities;
88

9-
namespace ChinesePinyinIntelliSenseExtender.Completion;
9+
namespace ChinesePinyinIntelliSenseExtender.Intellisense;
1010

1111
internal abstract class CompletionSourceBase
1212
{
@@ -38,29 +38,23 @@ public CompletionSourceBase(GeneralOptions options)
3838

3939
#region Protected 方法
4040

41-
protected static string FormatString(string? format, string origin, string spellings)
41+
protected static string FormatString(string? format, string param0, string param1)
4242
{
4343
if (string.IsNullOrEmpty(format))
4444
{
45-
return origin;
45+
return param0;
4646
}
4747
var builder = PooledStringBuilder.GetInstance();
4848
try
4949
{
50-
return builder.Builder.AppendFormat(format, origin, spellings).ToString();
50+
return builder.Builder.AppendFormat(format, param0, param1).ToString();
5151
}
5252
finally
5353
{
5454
builder.Free();
5555
}
5656
}
5757

58-
protected bool CanNotProcess()
59-
{
60-
return s_completionContextRecursionTag.Value
61-
|| !Options.Enable;
62-
}
63-
6458
protected InputMethodDictionaryGroup GetInputMethodDictionaryGroup()
6559
{
6660
if (_inputMethodDictionaryGroup is null

src/Completion/CompletionSourceProviderBase.cs renamed to src/Intellisense/CompletionSourceProviderBase.cs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
77
using System.Threading;
8-
using ChinesePinyinIntelliSenseExtender.Completion.Async;
8+
using ChinesePinyinIntelliSenseExtender.Intellisense.AsyncCompletion;
9+
using ChinesePinyinIntelliSenseExtender.Intellisense.SyncCompletion;
910
using ChinesePinyinIntelliSenseExtender.Options;
1011
using Microsoft.VisualStudio.Utilities;
1112

12-
namespace ChinesePinyinIntelliSenseExtender.Completion;
13+
namespace ChinesePinyinIntelliSenseExtender.Intellisense;
1314

1415
internal abstract class CompletionSourceProviderBase<TCompletionSourceDependence, TCompletionSource> where TCompletionSourceDependence : class where TCompletionSource : class
1516
{
@@ -43,49 +44,12 @@ internal abstract class CompletionSourceProviderBase<TCompletionSourceDependence
4344

4445
static CompletionSourceProviderBase()
4546
{
46-
s_providerContentTypeCache.TryAdd(typeof(PinyinAsyncCompletionSourceProvider), Array.Empty<string>());
47-
}
48-
49-
#endregion Public 构造函数
50-
51-
#region Public 方法
52-
53-
public virtual TCompletionSource GetOrCreate(TCompletionSourceDependence dependence)
54-
{
55-
if (s_recursionTag.Value)
56-
{
57-
Debug.WriteLine("Recursion call. Return EmptyAsyncCompletionSource.");
58-
return GetDefaultCompletionSource(dependence);
59-
}
60-
s_recursionTag.Value = true;
61-
62-
try
63-
{
64-
if (_completionSourceCache.TryGetValue(dependence, out var itemSource))
65-
{
66-
Debug.WriteLine($"CompletionSource cache hit by {dependence}.");
67-
return itemSource;
68-
}
69-
Debug.WriteLine($"No completionSource cache for {dependence}.");
70-
71-
if (CheckShouldIgnore(dependence))
72-
{
73-
return GetDefaultCompletionSource(dependence);
74-
}
75-
76-
var completionSource = CreateCompletionSource(dependence);
77-
78-
_completionSourceCache.TryAdd(dependence, completionSource);
47+
s_providerContentTypeCache.TryAdd(typeof(IdeographAsyncCompletionSourceProvider), Array.Empty<string>());
48+
s_providerContentTypeCache.TryAdd(typeof(IdeographCompletionSourceProvider), Array.Empty<string>());
7949

80-
return completionSource;
81-
}
82-
finally
83-
{
84-
s_recursionTag.Value = false;
85-
}
8650
}
8751

88-
#endregion Public 方法
52+
#endregion Public 构造函数
8953

9054
#region Protected 方法
9155

@@ -135,5 +99,40 @@ protected virtual bool CheckShouldIgnore(TCompletionSourceDependence dependence)
13599

136100
protected abstract TCompletionSource GetDefaultCompletionSource(TCompletionSourceDependence dependence);
137101

102+
protected virtual TCompletionSource GetOrCreateCompletionSource(TCompletionSourceDependence dependence)
103+
{
104+
if (s_recursionTag.Value)
105+
{
106+
Debug.WriteLine("Recursion call. Return EmptyAsyncCompletionSource.");
107+
return GetDefaultCompletionSource(dependence);
108+
}
109+
s_recursionTag.Value = true;
110+
111+
try
112+
{
113+
if (_completionSourceCache.TryGetValue(dependence, out var itemSource))
114+
{
115+
Debug.WriteLine($"CompletionSource cache hit by {dependence}.");
116+
return itemSource;
117+
}
118+
Debug.WriteLine($"No completionSource cache for {dependence}.");
119+
120+
if (CheckShouldIgnore(dependence))
121+
{
122+
return GetDefaultCompletionSource(dependence);
123+
}
124+
125+
var completionSource = CreateCompletionSource(dependence);
126+
127+
_completionSourceCache.TryAdd(dependence, completionSource);
128+
129+
return completionSource;
130+
}
131+
finally
132+
{
133+
s_recursionTag.Value = false;
134+
}
135+
}
136+
138137
#endregion Protected 方法
139138
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ChinesePinyinIntelliSenseExtender.Intellisense;
2+
3+
internal interface IIdeographCompletion
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ChinesePinyinIntelliSenseExtender.Intellisense;
2+
3+
internal interface IIdeographCompletionSet
4+
{
5+
}

0 commit comments

Comments
 (0)