Skip to content

Commit d4bb1c2

Browse files
author
SlavaRa
committed
Merge pull request #46 from SlavaRa/develop
Cleanup...
2 parents e1fd278 + d7dd3ba commit d4bb1c2

File tree

3 files changed

+93
-96
lines changed

3 files changed

+93
-96
lines changed

PostfixCodeCompletion/Helpers/TemplateUtils.cs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
54
using System.Text.RegularExpressions;
@@ -13,41 +12,40 @@
1312

1413
namespace PostfixCodeCompletion.Helpers
1514
{
16-
static class TemplateUtils
15+
internal static class TemplateUtils
1716
{
18-
public const string PATTERN_BLOCK = @"\$\([^\)]*{0}.*?\)";
19-
public const string PATTERN_T_BLOCK = @"[^\$]*?\$\({0}\)";
20-
internal const string POSTFIX_GENERATORS = "PostfixGenerators";
21-
internal const string PATTERN_MEMBER = "PCCMember";
22-
internal const string PATTERN_NULLABLE = "PCCNullable";
23-
internal const string PATTERN_COLLECTION = "PCCCollection";
24-
internal const string PATTERN_COLLECTION_KEY_TYPE = "$(CollectionKeyType)";
25-
internal const string PATTERN_COLLECTION_ITEM_TYPE = "$(CollectionItemType)";
26-
internal const string PATTERN_HASH = "PCCHash";
27-
internal const string PATTERN_BOOL = "PCCBoolean";
28-
internal const string PATTERN_NUMBER = "PCCNumber";
29-
internal const string PATTERN_STRING = "PCCString";
30-
internal const string PATTERN_TYPE = "PCCType";
17+
public const string PatternBlock = @"\$\([^\)]*{0}.*?\)";
18+
public const string PatternTBlock = @"[^\$]*?\$\({0}\)";
19+
internal const string PostfixGenerators = "PostfixGenerators";
20+
internal const string PatternMember = "PCCMember";
21+
internal const string PatternNullable = "PCCNullable";
22+
internal const string PatternCollection = "PCCCollection";
23+
internal const string PatternCollectionKeyType = "$(CollectionKeyType)";
24+
internal const string PatternCollectionItemType = "$(CollectionItemType)";
25+
internal const string PatternHash = "PCCHash";
26+
internal const string PatternBool = "PCCBoolean";
27+
internal const string PatternNumber = "PCCNumber";
28+
internal const string PatternString = "PCCString";
29+
internal const string PatternType = "PCCType";
3130
public static Settings Settings { get; set; }
3231

3332
static readonly List<string> Templates = new List<string>
3433
{
35-
PATTERN_MEMBER,
36-
PATTERN_NULLABLE,
37-
PATTERN_COLLECTION,
38-
PATTERN_HASH,
39-
PATTERN_BOOL,
40-
PATTERN_NUMBER,
41-
PATTERN_STRING,
42-
PATTERN_TYPE
34+
PatternMember,
35+
PatternNullable,
36+
PatternCollection,
37+
PatternHash,
38+
PatternBool,
39+
PatternNumber,
40+
PatternString,
41+
PatternType
4342
};
4443

4544
internal static bool GetHasTemplates() => GetHasTemplates(PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
4645

4746
internal static bool GetHasTemplates(string language)
4847
{
49-
return GetHasTemplates(PathHelper.SnippetDir, language)
50-
|| Settings.CustomSnippetDirectories.Any(it => GetHasTemplates(it.Path, language));
48+
return GetHasTemplates(PathHelper.SnippetDir, language) || Settings.CustomSnippetDirectories.Any(it => GetHasTemplates(it.Path, language));
5149
}
5250

5351
static bool GetHasTemplates(string snippetPath, string language)
@@ -58,11 +56,11 @@ static bool GetHasTemplates(string snippetPath, string language)
5856

5957
static string GetTemplatesDir(string snippetPath) => GetTemplatesDir(snippetPath, PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
6058

61-
static string GetTemplatesDir(string snippetPath, string language) => Path.Combine(Path.Combine(snippetPath, language), POSTFIX_GENERATORS);
59+
static string GetTemplatesDir(string snippetPath, string language) => Path.Combine(Path.Combine(snippetPath, language), PostfixGenerators);
6260

6361
internal static Dictionary<string, string> GetTemplates(string type)
6462
{
65-
var pattern = Templates.Contains(type) ? string.Format(PATTERN_BLOCK, type) : string.Format(PATTERN_T_BLOCK, type);
63+
var pattern = Templates.Contains(type) ? string.Format(PatternBlock, type) : string.Format(PatternTBlock, type);
6664
var result = new Dictionary<string, string>();
6765
var paths = Settings.CustomSnippetDirectories.Select(it => GetTemplatesDir(it.Path)).ToList();
6866
paths.Add(GetTemplatesDir(PathHelper.SnippetDir));
@@ -73,13 +71,13 @@ internal static Dictionary<string, string> GetTemplates(string type)
7371
{
7472
var content = GetFileContent(file);
7573
var marker = $"#pcc:{type}";
76-
var startIndex = content.IndexOf(marker, StringComparison.Ordinal);
74+
var startIndex = content.IndexOfOrdinal(marker);
7775
if (startIndex != -1)
7876
{
7977
startIndex += marker.Length;
8078
content = content.Remove(0, startIndex);
8179
}
82-
startIndex = content.IndexOf("#pcc:", StringComparison.Ordinal);
80+
startIndex = content.IndexOfOrdinal("#pcc:");
8381
if (startIndex != -1) content = content.Remove(startIndex);
8482
if (!Regex.IsMatch(content, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)) continue;
8583
result.Add(file, content.Replace("\r\n", "\n"));
@@ -125,7 +123,7 @@ internal static KeyValuePair<string, string> GetVarNameToQualifiedName(ASResult
125123
internal static string ProcessMemberTemplate(string template, ASResult expr)
126124
{
127125
var varNameToQualifiedName = GetVarNameToQualifiedName(expr);
128-
var name = varNameToQualifiedName.Key.ToLower();
126+
var name = varNameToQualifiedName.Key;
129127
var type = varNameToQualifiedName.Value;
130128
template = ASCompletion.Completion.TemplateUtils.ReplaceTemplateVariable(template, "Name", name);
131129
if (ASContext.Context is Context && Settings != null && Settings.DisableTypeDeclaration) type = null;
@@ -145,10 +143,10 @@ internal static string ProcessCollectionTemplate(string template, ASResult expr)
145143
case "as2":
146144
case "as3":
147145
if (string.IsNullOrEmpty(type)) type = ASContext.Context.Features.dynamicKey;
148-
template = template.Replace(PATTERN_COLLECTION_KEY_TYPE, "int");
146+
template = template.Replace(PatternCollectionKeyType, "int");
149147
break;
150148
}
151-
return template.Replace(PATTERN_COLLECTION_ITEM_TYPE, type);
149+
return template.Replace(PatternCollectionItemType, type);
152150
}
153151

154152
internal static string ProcessHashTemplate(string template, ASResult expr)
@@ -163,8 +161,8 @@ internal static string ProcessHashTemplate(string template, ASResult expr)
163161
var objectKey = features.objectKey;
164162
if (type == objectKey || type == "Dictionary")
165163
{
166-
template = template.Replace(PATTERN_COLLECTION_KEY_TYPE, type == objectKey ? features.stringKey : objectKey);
167-
template = template.Replace(PATTERN_COLLECTION_ITEM_TYPE, features.dynamicKey);
164+
template = template.Replace(PatternCollectionKeyType, type == objectKey ? features.stringKey : objectKey);
165+
template = template.Replace(PatternCollectionItemType, features.dynamicKey);
168166
}
169167
break;
170168
}
@@ -180,7 +178,7 @@ internal static string GetDescription(ASResult expr, string template, string pcc
180178
var line = sci.GetLine(lineNum);
181179
var snippet = line.Substring(exprStartPosition - sci.PositionFromLine(lineNum), position - exprStartPosition);
182180
var result = template.Replace(SnippetHelper.BOUNDARY, string.Empty);
183-
result = Regex.Replace(result, string.Format(PATTERN_BLOCK, pccpattern), snippet, RegexOptions.IgnoreCase | RegexOptions.Multiline);
181+
result = Regex.Replace(result, string.Format(PatternBlock, pccpattern), snippet, RegexOptions.IgnoreCase | RegexOptions.Multiline);
184182
result = ProcessMemberTemplate(result, expr);
185183
result = ArgsProcessor.ProcessCodeStyleLineBreaks(result);
186184
result = result.Replace(SnippetHelper.ENTRYPOINT, "|");
@@ -196,7 +194,7 @@ internal static void InsertSnippetText(ASResult expr, string template, string pc
196194
sci.ReplaceSel(string.Empty);
197195
position = ScintillaControlHelper.GetExpressionStartPosition(sci, sci.CurrentPos, expr);
198196
sci.SetSel(position, sci.CurrentPos);
199-
var snippet = Regex.Replace(template, string.Format(PATTERN_BLOCK, pccpattern), sci.SelText, RegexOptions.IgnoreCase | RegexOptions.Multiline);
197+
var snippet = Regex.Replace(template, string.Format(PatternBlock, pccpattern), sci.SelText, RegexOptions.IgnoreCase | RegexOptions.Multiline);
200198
snippet = ProcessMemberTemplate(snippet, expr);
201199
snippet = ArgsProcessor.ProcessCodeStyleLineBreaks(snippet);
202200
sci.ReplaceSel(string.Empty);

PostfixCodeCompletion/PluginMain.cs

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ public class PluginMain : IPlugin
3232

3333
#region Required Properties
3434

35-
public int Api { get { return 1; }}
35+
public int Api => 1;
3636

37-
public string Name { get { return "PostfixCodeCompletion"; }}
37+
public string Name => "PostfixCodeCompletion";
3838

39-
public string Guid { get { return "21d9ab3e-93e4-4460-9298-c62f87eed7ba"; }}
39+
public string Guid => "21d9ab3e-93e4-4460-9298-c62f87eed7ba";
4040

41-
public string Help { get { return ""; }}
41+
public string Help => string.Empty;
4242

43-
public string Author { get { return "SlavaRa"; }}
43+
public string Author => "SlavaRa";
4444

45-
public string Description { get { return "Postfix code completion helps reduce backward caret jumps as you write code"; }}
45+
public string Description => "Postfix code completion helps reduce backward caret jumps as you write code";
4646

4747
public object Settings { get; private set; }
4848

@@ -149,13 +149,13 @@ void AddEventHandlers()
149149
/// </summary>
150150
void SaveSettings() => ObjectSerializer.Serialize(settingFilename, Settings);
151151

152-
static void UpdateCompletionList()
152+
void UpdateCompletionList()
153153
{
154154
var expr = GetPostfixCompletionExpr();
155155
UpdateCompletionList(expr);
156156
}
157157

158-
static void UpdateCompletionList(ASResult expr)
158+
void UpdateCompletionList(ASResult expr)
159159
{
160160
if (expr == null || expr.IsNull()) return;
161161
var target = GetPostfixCompletionTarget(expr);
@@ -171,7 +171,7 @@ static void UpdateCompletionList(ASResult expr)
171171
hc.GetPositionType(OnFunctionTypeResult);
172172
}
173173

174-
static void UpdateCompletionList(MemberModel target, ASResult expr)
174+
void UpdateCompletionList(MemberModel target, ASResult expr)
175175
{
176176
if (target == null || !TemplateUtils.GetHasTemplates()) return;
177177
var items = GetPostfixCompletionItems(target, expr);
@@ -248,32 +248,32 @@ static MemberModel GetPostfixCompletionTarget(ASResult expr)
248248
return null;
249249
}
250250

251-
static List<ICompletionListItem> GetPostfixCompletionItems(MemberModel target, ASResult expr)
251+
List<ICompletionListItem> GetPostfixCompletionItems(MemberModel target, ASResult expr)
252252
{
253253
var result = new List<ICompletionListItem>();
254254
if (expr.Member != null) result.AddRange(GetCompletionItems(expr.Member.Type, target, expr));
255255
else if (expr.Type != null) result.AddRange(GetCompletionItems(expr.Type.Type, target, expr));
256-
result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_MEMBER, expr));
257-
if (IsNullable(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_NULLABLE, expr));
258-
if (IsCollection(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_COLLECTION, expr));
259-
if (IsHash(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_HASH, expr));
256+
result.AddRange(GetCompletionItems(TemplateUtils.PatternMember, expr));
257+
if (IsNullable(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternNullable, expr));
258+
if (IsCollection(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternCollection, expr));
259+
if (IsHash(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternHash, expr));
260260
if (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower() == "haxe")
261261
{
262262
var type = !string.IsNullOrEmpty(expr.Type?.Type) && expr.Type.Type != ASContext.Context.Features.voidKey ? expr.Type : null;
263263
if (type != null)
264264
{
265-
if (IsCollection(type)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_COLLECTION, expr));
266-
if (IsHash(type)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_HASH, expr));
265+
if (IsCollection(type)) result.AddRange(GetCompletionItems(TemplateUtils.PatternCollection, expr));
266+
if (IsHash(type)) result.AddRange(GetCompletionItems(TemplateUtils.PatternHash, expr));
267267
}
268268
}
269-
if (IsBoolean(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_BOOL, expr));
270-
if (IsNumber(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_NUMBER, expr));
271-
if (IsString(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_STRING, expr));
272-
if (IsType(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_TYPE, expr));
269+
if (IsBoolean(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternBool, expr));
270+
if (IsNumber(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternNumber, expr));
271+
if (IsString(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternString, expr));
272+
if (IsType(target)) result.AddRange(GetCompletionItems(TemplateUtils.PatternType, expr));
273273
return result.Distinct().ToList();
274274
}
275275

276-
static bool IsNullable(MemberModel target) => !IsNumber(target) && target.Type != ASContext.Context.Features.booleanKey;
276+
bool IsNullable(MemberModel target) => !IsNumber(target) && target.Type != ASContext.Context.Features.booleanKey;
277277

278278
static bool IsCollection(MemberModel target)
279279
{
@@ -326,20 +326,13 @@ static bool IsHash(MemberModel target)
326326

327327
static bool IsBoolean(MemberModel target) => target.Type == ASContext.Context.Features.booleanKey;
328328

329-
static bool IsNumber(MemberModel target)
329+
bool IsNumber(MemberModel target)
330330
{
331331
var type = target is ClassModel ? ((ClassModel)target).QualifiedName : target.Type;
332332
if (type == ASContext.Context.Features.numberKey) return true;
333-
switch (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage)
334-
{
335-
case "as2":
336-
case "as3":
337-
return type == "int" || type == "uint";
338-
case "haxe":
339-
return type == "Int" || type == "UInt";
340-
default:
341-
return false;
342-
}
333+
var language = PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower();
334+
var features = ((Settings)Settings).LanguageFeatures.First(it => it.Language == language);
335+
return features != null && features.Numeric.Contains(type);
343336
}
344337

345338
static bool IsString(MemberModel target) => target.Type == ASContext.Context.Features.stringKey;
@@ -383,10 +376,10 @@ static IEnumerable<ICompletionListItem> GetCompletionItems(Dictionary<string, st
383376
var template = pathToTemplate.Value;
384377
switch (pattern)
385378
{
386-
case TemplateUtils.PATTERN_COLLECTION:
379+
case TemplateUtils.PatternCollection:
387380
template = TemplateUtils.ProcessCollectionTemplate(template, expr);
388381
break;
389-
case TemplateUtils.PATTERN_HASH:
382+
case TemplateUtils.PatternHash:
390383
template = TemplateUtils.ProcessHashTemplate(template, expr);
391384
break;
392385
}
@@ -426,7 +419,7 @@ static Process CreateHaxeProcess(string args)
426419

427420
#region Event Handlers
428421

429-
static void OnCharAdded(ScintillaControl sender, int value)
422+
void OnCharAdded(ScintillaControl sender, int value)
430423
{
431424
try
432425
{
@@ -451,14 +444,14 @@ static void OnCharAdded(ScintillaControl sender, int value)
451444
}
452445
}
453446

454-
static void OnCompletionListVisibleChanged(object o, EventArgs args)
447+
void OnCompletionListVisibleChanged(object o, EventArgs args)
455448
{
456449
var list = Reflector.CompletionList.CompletionList;
457450
if (list.Visible) UpdateCompletionList();
458451
else list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
459452
}
460453

461-
static void OnCompletionListSelectedValueChanged(object sender, EventArgs args)
454+
void OnCompletionListSelectedValueChanged(object sender, EventArgs args)
462455
{
463456
var list = Reflector.CompletionList.CompletionList;
464457
list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
@@ -517,7 +510,7 @@ static void OnHaxeContextFallbackNeeded(bool notSupported)
517510
completionModeHandler = new CompilerCompletionHandler(CreateHaxeProcess(string.Empty));
518511
}
519512

520-
static void OnFunctionTypeResult(HaxeComplete hc, HaxeCompleteResult result, HaxeCompleteStatus status)
513+
void OnFunctionTypeResult(HaxeComplete hc, HaxeCompleteResult result, HaxeCompleteStatus status)
521514
{
522515
switch (status)
523516
{
@@ -549,7 +542,7 @@ static void OnFunctionTypeResult(HaxeComplete hc, HaxeCompleteResult result, Hax
549542
#endregion
550543
}
551544

552-
class PostfixCompletionItem : ICompletionListItem
545+
internal class PostfixCompletionItem : ICompletionListItem
553546
{
554547
readonly string template;
555548
readonly ASResult expr;
@@ -561,12 +554,12 @@ public PostfixCompletionItem(string label, string template, ASResult expr)
561554
this.expr = expr;
562555
}
563556

564-
public string Label { get; set; }
557+
public string Label { get; }
565558

566559
string pattern;
567560
public virtual string Pattern
568561
{
569-
get { return pattern ?? TemplateUtils.PATTERN_MEMBER; }
562+
get { return pattern ?? TemplateUtils.PatternMember; }
570563
set { pattern = value; }
571564
}
572565

@@ -587,15 +580,9 @@ public Bitmap Icon
587580
}
588581

589582
string description;
590-
public string Description
591-
{
592-
get
593-
{
594-
return description ?? (description = TemplateUtils.GetDescription(expr, template, Pattern));
595-
}
596-
}
583+
public string Description => description ?? (description = TemplateUtils.GetDescription(expr, template, Pattern));
597584

598-
public new string ToString() { return Description; }
585+
public new string ToString() => Description;
599586

600587
/// <summary>
601588
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
@@ -618,9 +605,6 @@ public override bool Equals(object obj)
618605
/// A hash code for the current <see cref="T:System.Object"/>.
619606
/// </returns>
620607
/// <filterpriority>2</filterpriority>
621-
public override int GetHashCode()
622-
{
623-
return Label.GetHashCode() ^ expr.GetHashCode();
624-
}
608+
public override int GetHashCode() => Label.GetHashCode() ^ expr.GetHashCode();
625609
}
626610
}

0 commit comments

Comments
 (0)