Skip to content

Commit c4aafd3

Browse files
author
slavara
committed
Cleanup...
1 parent d7e2732 commit c4aafd3

File tree

4 files changed

+42
-77
lines changed

4 files changed

+42
-77
lines changed

PostfixCodeCompletion/Helpers/ScintillaControlHelper.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,20 @@ internal static int GetExpressionStartPosition(ScintillaControl sci, int positio
3939
if (sQuotes == 0) sQuotes++;
4040
else sQuotes--;
4141
}
42-
else if (arrCount == 0 && parCount == 0 && genCount == 0 && braCount == 0
43-
&& dQuotes == 0 && sQuotes == 0
44-
&& !characters.Contains(c) && c != '.')
42+
else if (arrCount == 0 && parCount == 0 && genCount == 0 && braCount == 0 && dQuotes == 0 && sQuotes == 0 && !characters.Contains(c) && c != '.')
4543
{
4644
result = i;
4745
break;
4846
}
4947
}
5048
if (expr.Member == null && sci.GetWordLeft(result - 1, true) == "new")
51-
return GetWordLeftStartPosition(sci, result - 1, true);
49+
result = sci.WordStartPosition(result - 1, false);
5250
return result;
5351
}
5452

55-
internal static int GetWordLeftStartPosition(ScintillaControl sci, int position, bool skipWhiteSpace)
53+
internal static int GetWordLeftStartPosition(ScintillaControl sci, int position)
5654
{
55+
var skipWhiteSpace = true;
5756
var characters = ScintillaControl.Configuration.GetLanguage(sci.ConfigurationLanguage).characterclass.Characters;
5857
while (position >= 0)
5958
{

PostfixCodeCompletion/Helpers/TemplateUtils.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ static class TemplateUtils
4242
PATTERN_TYPE
4343
};
4444

45-
internal static bool GetHasTemplates()
46-
{
47-
return GetHasTemplates(PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
48-
}
45+
internal static bool GetHasTemplates() => GetHasTemplates(PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
4946

5047
internal static bool GetHasTemplates(string language)
5148
{
@@ -59,17 +56,9 @@ static bool GetHasTemplates(string snippetPath, string language)
5956
return Directory.Exists(snippetPath) && Directory.GetFiles(snippetPath, "*.fds").Length > 0;
6057
}
6158

62-
static string GetTemplatesDir(string snippetPath)
63-
{
64-
return GetTemplatesDir(snippetPath, PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
65-
}
59+
static string GetTemplatesDir(string snippetPath) => GetTemplatesDir(snippetPath, PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
6660

67-
static string GetTemplatesDir(string snippetPath, string language)
68-
{
69-
snippetPath = Path.Combine(snippetPath, language);
70-
snippetPath = Path.Combine(snippetPath, POSTFIX_GENERATORS);
71-
return snippetPath;
72-
}
61+
static string GetTemplatesDir(string snippetPath, string language) => Path.Combine(Path.Combine(snippetPath, language), POSTFIX_GENERATORS);
7362

7463
internal static Dictionary<string, string> GetTemplates(string type)
7564
{
@@ -113,20 +102,18 @@ static string GetFileContent(string file)
113102
internal static KeyValuePair<string, string> GetVarNameToQualifiedName(ASResult expr)
114103
{
115104
string type = null;
116-
var varname = string.Empty;
117-
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
118-
var lineNum = sci.CurrentLine;
119-
var line = sci.GetLine(lineNum);
120-
var returnType = Reflector.ASGenerator.GetStatementReturnType(sci, line, sci.PositionFromLine(lineNum));
121-
var word = returnType?.Word;
122105
var member = expr.Member;
123-
if (member != null && member.Type != null) type = member.Type;
106+
if (member?.Type != null) type = member.Type;
124107
else
125108
{
126109
var cType = expr.Type;
127-
if (cType != null && cType.Name != null) type = cType.QualifiedName;
110+
if (cType?.Name != null) type = cType.QualifiedName;
128111
}
129-
if (member != null && member.Name != null) varname = Reflector.ASGenerator.GuessVarName(member.Name, type);
112+
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
113+
var lineNum = sci.CurrentLine;
114+
var word = Reflector.ASGenerator.GetStatementReturnType(sci, sci.GetLine(lineNum), sci.PositionFromLine(lineNum))?.Word;
115+
var varname = string.Empty;
116+
if (member?.Name != null) varname = Reflector.ASGenerator.GuessVarName(member.Name, type);
130117
if (!string.IsNullOrEmpty(word) && char.IsDigit(word[0])) word = null;
131118
if (!string.IsNullOrEmpty(word) && (string.IsNullOrEmpty(type) || Regex.IsMatch(type, "(<[^]]+>)"))) word = null;
132119
if (!string.IsNullOrEmpty(type) && type == ASContext.Context.Features.voidKey) type = null;
@@ -153,7 +140,7 @@ internal static string ProcessCollectionTemplate(string template, ASResult expr)
153140
if (type.Contains("@")) type = $"{type.Replace("@", ".<")}>";
154141
type = Regex.Match(type, "<([^]]+)>").Groups[1].Value;
155142
type = Reflector.ASGenerator.GetShortType(type);
156-
switch (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage)
143+
switch (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower())
157144
{
158145
case "as2":
159146
case "as3":
@@ -166,7 +153,7 @@ internal static string ProcessCollectionTemplate(string template, ASResult expr)
166153

167154
internal static string ProcessHashTemplate(string template, ASResult expr)
168155
{
169-
switch (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage)
156+
switch (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower())
170157
{
171158
case "as2":
172159
case "as3":

PostfixCodeCompletion/PluginMain.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,19 @@ static void UpdateCompletionList(ASResult expr)
173173

174174
static void UpdateCompletionList(MemberModel target, ASResult expr)
175175
{
176-
if (target == null) return;
177-
if (!TemplateUtils.GetHasTemplates()) return;
176+
if (target == null || !TemplateUtils.GetHasTemplates()) return;
178177
var items = GetPostfixCompletionItems(target, expr);
179178
var allItems = Reflector.CompletionList.allItems;
180179
if (allItems != null)
181180
{
182181
var labels = new HashSet<string>();
183-
foreach (var item in allItems.OfType<PostfixCompletionItem>())
182+
foreach (var item in allItems)
184183
{
185-
labels.Add(item.Label);
184+
if (item is PostfixCompletionItem) labels.Add(item.Label);
186185
}
187186
foreach (var item in items)
188187
{
189-
var label = item.Label;
190-
if (!labels.Contains(label)) allItems.Add(item);
188+
if (!labels.Contains(item.Label)) allItems.Add(item);
191189
}
192190
items = allItems;
193191
}
@@ -243,8 +241,7 @@ static MemberModel GetPostfixCompletionTarget(ASResult expr)
243241
if (expr == null || expr.IsNull()) return null;
244242
var member = expr.Member;
245243
var voidKey = ASContext.Context.Features.voidKey;
246-
if (member != null && !string.IsNullOrEmpty(member.Type) && member.Type != voidKey)
247-
return member;
244+
if (!string.IsNullOrEmpty(member?.Type) && member.Type != voidKey) return member;
248245
var type = expr.Type;
249246
if (type != null && !type.IsVoid() && !string.IsNullOrEmpty(type.Type) && type.Type != voidKey)
250247
return type;
@@ -262,10 +259,7 @@ static List<ICompletionListItem> GetPostfixCompletionItems(MemberModel target, A
262259
if (IsHash(target)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_HASH, expr));
263260
if (PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower() == "haxe")
264261
{
265-
var type = expr.Type != null && !string.IsNullOrEmpty(expr.Type.Type) &&
266-
expr.Type.Type != ASContext.Context.Features.voidKey
267-
? expr.Type
268-
: null;
262+
var type = !string.IsNullOrEmpty(expr.Type?.Type) && expr.Type.Type != ASContext.Context.Features.voidKey ? expr.Type : null;
269263
if (type != null)
270264
{
271265
if (IsCollection(type)) result.AddRange(GetCompletionItems(TemplateUtils.PATTERN_COLLECTION, expr));
@@ -308,13 +302,18 @@ static bool IsHash(MemberModel target)
308302
var type = target.Type;
309303
return type == ASContext.Context.Features.objectKey || type == "Dictionary";
310304
case "haxe":
311-
if (IsIteratorOrIterable(target)) return true;
305+
Func<MemberModel, bool> isIterator = member =>
306+
{
307+
var cleanType = Reflector.ASGenerator.CleanType(member.Type);
308+
return cleanType == "Iterator" || cleanType == "Iterable";
309+
};
310+
if (isIterator(target)) return true;
312311
if (target is ClassModel)
313312
{
314313
var classModel = target as ClassModel;
315314
while (classModel != null && !classModel.IsVoid())
316315
{
317-
if (classModel.Members.Items.Any(IsIteratorOrIterable))
316+
if (classModel.Members.Items.Any(isIterator))
318317
return true;
319318
classModel.ResolveExtends();
320319
classModel = classModel.Extends;
@@ -325,12 +324,6 @@ static bool IsHash(MemberModel target)
325324
}
326325
}
327326

328-
static bool IsIteratorOrIterable(MemberModel member)
329-
{
330-
var cleanType = Reflector.ASGenerator.CleanType(member.Type);
331-
return cleanType == "Iterator" || cleanType == "Iterable";
332-
}
333-
334327
static bool IsBoolean(MemberModel target) => target.Type == ASContext.Context.Features.booleanKey;
335328

336329
static bool IsNumber(MemberModel target)
@@ -368,10 +361,7 @@ static IEnumerable<ICompletionListItem> GetCompletionItems(string pattern, Membe
368361
return GetCompletionItems(templates ?? new Dictionary<string, string>(), pattern, expr);
369362
}
370363

371-
static IEnumerable<ICompletionListItem> GetCompletionItems(string pattern, ASResult expr)
372-
{
373-
return GetCompletionItems(TemplateUtils.GetTemplates(pattern), pattern, expr);
374-
}
364+
static IEnumerable<ICompletionListItem> GetCompletionItems(string pattern, ASResult expr) => GetCompletionItems(TemplateUtils.GetTemplates(pattern), pattern, expr);
375365

376366
static IEnumerable<ICompletionListItem> GetCompletionItems(Dictionary<string, string> templates, string pattern, ASResult expr)
377367
{
@@ -400,10 +390,7 @@ static IEnumerable<ICompletionListItem> GetCompletionItems(Dictionary<string, st
400390
template = TemplateUtils.ProcessHashTemplate(template, expr);
401391
break;
402392
}
403-
var item = new PostfixCompletionItem(fileName, template, expr)
404-
{
405-
Pattern = pattern
406-
};
393+
var item = new PostfixCompletionItem(fileName, template, expr) {Pattern = pattern};
407394
if (isHaxe && fileName == "code" && itemIcon != null)
408395
{
409396
item.Icon = itemIcon;

PostfixCodeCompletionTests/PostfixCodeCompletionTests.csproj

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>09ffa086-ab00-483f-8da6-c3be5f20a1f7</ProjectGuid>
7+
<ProjectGuid>{09FFA086-AB00-483F-8DA6-C3BE5F20A1F7}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>PostfixCodeCompletionTests</RootNamespace>
@@ -30,23 +30,16 @@
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
3232
<ItemGroup>
33-
<Reference Include="System"/>
34-
35-
<Reference Include="System.Core"/>
36-
<Reference Include="System.Xml.Linq"/>
37-
<Reference Include="System.Data.DataSetExtensions"/>
38-
39-
40-
<Reference Include="Microsoft.CSharp"/>
41-
42-
<Reference Include="System.Data"/>
43-
44-
<Reference Include="System.Net.Http"/>
45-
46-
<Reference Include="System.Xml"/>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Net.Http" />
40+
<Reference Include="System.Xml" />
4741
</ItemGroup>
4842
<ItemGroup>
49-
<Compile Include="Class1.cs" />
5043
<Compile Include="Properties\AssemblyInfo.cs" />
5144
</ItemGroup>
5245
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -57,5 +50,4 @@
5750
<Target Name="AfterBuild">
5851
</Target>
5952
-->
60-
61-
</Project>
53+
</Project>

0 commit comments

Comments
 (0)