Skip to content

Commit c031c58

Browse files
committed
Refactored some functions into a utility class - it's only the beginning.
1 parent a0b18a9 commit c031c58

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

External/Plugins/ASCompletion/ASCompletion.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="Completion\ArgumentsProcessor.cs" />
7878
<Compile Include="Completion\ASComplete.cs" />
7979
<Compile Include="Completion\ASGenerator.cs" />
80+
<Compile Include="Completion\CodeUtils.cs" />
8081
<Compile Include="Completion\Reformater.cs" />
8182
<Compile Include="Completion\TemplateUtils.cs" />
8283
<Compile Include="Context\ASContext.cs" />

External/Plugins/ASCompletion/Completion/ASComplete.cs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,13 +1229,13 @@ static private bool HandleDeclarationCompletion(ScintillaControl Sci, string tai
12291229
while (tempLine > 0)
12301230
{
12311231
tempText = Sci.GetLine(tempLine).Trim();
1232-
if (insideClass && IsTypeDecl(tempText, features.typesKeywords))
1232+
if (insideClass && CodeUtils.IsTypeDecl(tempText, features.typesKeywords))
12331233
{
12341234
tempIndent = Sci.GetLineIndentation(tempLine);
12351235
tab = tempIndent + Sci.TabWidth;
12361236
break;
12371237
}
1238-
if (tempText.Length > 0 && (tempText.EndsWith("}") || IsDeclaration(tempText, features)))
1238+
if (tempText.Length > 0 && (tempText.EndsWith("}") || CodeUtils.IsDeclaration(tempText, features)))
12391239
{
12401240
tempIndent = Sci.GetLineIndentation(tempLine);
12411241
tab = tempIndent;
@@ -1260,45 +1260,6 @@ static private bool HandleDeclarationCompletion(ScintillaControl Sci, string tai
12601260
return true;
12611261
}
12621262

1263-
/// <summary>
1264-
/// Lookup type declaration keywords anywhere in the provided text
1265-
/// </summary>
1266-
private static bool IsTypeDecl(string line, string[] typesKeywords)
1267-
{
1268-
var max = line.Length - 1;
1269-
foreach (string keyword in typesKeywords)
1270-
{
1271-
var p = line.IndexOf(keyword);
1272-
if (p >= 0)
1273-
{
1274-
// verify keyword between spaces
1275-
var end = p + keyword.Length;
1276-
if ((p == 0 || line[p-1] <= 32)
1277-
&& end < max && line[end] <= 32) return true;
1278-
}
1279-
}
1280-
return false;
1281-
}
1282-
1283-
/// <summary>
1284-
/// Look if the provided text starts with any declaration keyword
1285-
/// </summary>
1286-
private static bool IsDeclaration(string line, ContextFeatures features)
1287-
{
1288-
foreach (string keyword in features.accessKeywords)
1289-
if (line.StartsWith(keyword) && SpaceFollows(line, keyword)) return true;
1290-
foreach (string keyword in features.declKeywords)
1291-
if (line.StartsWith(keyword) && SpaceFollows(line, keyword)) return true;
1292-
return false;
1293-
}
1294-
1295-
private static bool SpaceFollows(string line, string keyword)
1296-
{
1297-
var len = keyword.Length;
1298-
if (line.Length > len) return line[len] <= 32;
1299-
else return true;
1300-
}
1301-
13021263
#endregion
13031264

13041265
#region function_completion
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ASCompletion.Completion
7+
{
8+
static class CodeUtils
9+
{
10+
/// <summary>
11+
/// Lookup type declaration keywords anywhere in the provided text
12+
/// </summary>
13+
public static bool IsTypeDecl(string line, string[] typesKeywords)
14+
{
15+
var max = line.Length - 1;
16+
foreach (string keyword in typesKeywords)
17+
{
18+
var p = line.IndexOf(keyword);
19+
if (p >= 0) return IsSpaceAt(line, p - 1) && IsSpaceAt(line, p + keyword.Length);
20+
}
21+
return false;
22+
}
23+
24+
/// <summary>
25+
/// Look if the provided text starts with any declaration keyword
26+
/// </summary>
27+
public static bool IsDeclaration(string line, ContextFeatures features)
28+
{
29+
foreach (string keyword in features.accessKeywords)
30+
if (line.StartsWith(keyword) && IsSpaceAt(line, keyword.Length)) return true;
31+
foreach (string keyword in features.declKeywords)
32+
if (line.StartsWith(keyword) && IsSpaceAt(line, keyword.Length)) return true;
33+
return false;
34+
}
35+
36+
/// <summary>
37+
/// Look if character after first word of line is whitespace
38+
/// </summary>
39+
public static bool IsSpaceAt(string line, int index)
40+
{
41+
if (index < 0 || index >= line.Length) return true;
42+
else return line[index] <= 32;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)