Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 1f71e4f

Browse files
committed
Fix crash + Cleanup Methodmaps and Enum structs
1 parent 86203b0 commit 1f71e4f

File tree

10 files changed

+77
-80
lines changed

10 files changed

+77
-80
lines changed

SourcepawnCondenser/CondenserTest/MainWindow.xaml.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
145145
item.MouseLeftButtonUp += ItemMM_MouseLeftButtonUp;
146146
item.Items.Add(new TreeViewItem() { Header = "Index: " + m.Index, Background = Brushes.LightGray });
147147
item.Items.Add(new TreeViewItem() { Header = "Length: " + m.Length });
148-
item.Items.Add(new TreeViewItem() { Header = "Type: " + m.Type, Background = Brushes.LightGray });
149148
item.Items.Add(new TreeViewItem() { Header = "InheritedType: " + m.InheritedType });
150149
var subItem = new TreeViewItem() { Header = "Methods", Background = Brushes.LightGray };
151150
for (var j = 0; j < m.Methods.Count; ++j)
@@ -155,16 +154,6 @@ private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
155154
subSubItem.Items.Add(new TreeViewItem() { Header = "Length: " + m.Methods[j].Length, Background = Brushes.LightGray });
156155
subSubItem.Items.Add(new TreeViewItem() { Header = "Comment: >>" + m.Methods[j].CommentString + "<<" });
157156
subSubItem.Items.Add(new TreeViewItem() { Header = "Return: " + m.Methods[j].ReturnType, Background = Brushes.LightGray });
158-
var k = 0;
159-
for (; k < m.Methods[j].MethodKind.Length; ++k)
160-
{
161-
subSubItem.Items.Add(new TreeViewItem() { Header = "MethodKind" + (k + 1) + ": " + m.Methods[j].MethodKind[k], Background = (k % 2 == 0) ? Brushes.LightGray : Brushes.White });
162-
}
163-
for (var l = 0; l < m.Methods[j].Parameters.Length; ++l)
164-
{
165-
++k;
166-
subSubItem.Items.Add(new TreeViewItem() { Header = "Parameter" + (l + 1) + ": " + m.Methods[j].Parameters[l], Background = (k % 2 == 0) ? Brushes.LightGray : Brushes.White });
167-
}
168157
subItem.Items.Add(subSubItem);
169158
}
170159
item.Items.Add(subItem);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using SourcepawnCondenser.SourcemodDefinition;
3+
4+
namespace SourcepawnCondenser;
5+
6+
7+
/// <summary>
8+
/// This is inherited from from SMMethodmap and SMEnumStruct
9+
/// </summary>
10+
public abstract class SMClasslike : SMBaseDefinition
11+
{
12+
public List<SMObjectField> Fields = new();
13+
public List<SMObjectMethod> Methods = new();
14+
}
15+
16+
public abstract class SMObjectMethod : SMBaseDefinition
17+
{
18+
public string ClassName = string.Empty;
19+
public string FullName = string.Empty;
20+
public string ReturnType = string.Empty;
21+
}
22+
23+
public abstract class SMObjectField : SMBaseDefinition
24+
{
25+
public string ClassName = string.Empty;
26+
public string FullName = string.Empty;
27+
}

SourcepawnCondenser/SourcepawnCondenser/CondenserFunctions/SMMethodmapConsumer.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ private int ConsumeSMMethodmap()
1515
if ((position + 4) < length)
1616
{
1717
var methodMapName = string.Empty;
18-
var methodMapType = string.Empty;
1918
var methods = new List<SMMethodmapMethod>();
2019
var fields = new List<SMMethodmapField>();
2120
if (t[iteratePosition].Kind == TokenKind.Identifier)
2221
{
22+
methodMapName = t[iteratePosition].Value;
23+
24+
// Handle declaration like "methodmap MyMap __nullable { ... }"
2325
if (t[iteratePosition + 1].Kind == TokenKind.Identifier)
2426
{
25-
methodMapType = t[iteratePosition].Value;
2627
++iteratePosition;
27-
methodMapName = t[iteratePosition].Value;
28-
}
29-
else
30-
{
31-
methodMapName = t[iteratePosition].Value;
3228
}
3329
++iteratePosition;
3430
}
@@ -97,7 +93,7 @@ private int ConsumeSMMethodmap()
9793
var functionIndicators = new List<string>();
9894
var parameters = new List<string>();
9995
var methodName = string.Empty;
100-
var methodReturnValue = string.Empty;
96+
var methodReturnType = string.Empty;
10197
var ParsingIndicators = true;
10298
var InCodeSection = false;
10399
var ParenthesisIndex = 0;
@@ -144,7 +140,7 @@ private int ConsumeSMMethodmap()
144140
{
145141
if (t[i + 1].Kind == TokenKind.Identifier)
146142
{
147-
methodReturnValue = t[i].Value;
143+
methodReturnType = t[i].Value;
148144
methodName = t[i + 1].Value;
149145
++i;
150146
}
@@ -210,13 +206,13 @@ private int ConsumeSMMethodmap()
210206
{
211207
Index = mStartIndex,
212208
Name = methodName,
213-
ReturnType = methodReturnValue,
214-
MethodKind = functionIndicators.ToArray(),
215-
Parameters = parameters.ToArray(),
209+
ReturnType = methodReturnType,
210+
/*MethodKind = functionIndicators.ToArray(),
211+
Parameters = parameters.ToArray(),*/
216212
FullName = TrimFullname(source.Substring(mStartIndex, mEndIndex - mStartIndex + 1)),
217213
Length = mEndIndex - mStartIndex + 1,
218214
CommentString = TrimComments(functionCommentString),
219-
MethodmapName = methodMapName,
215+
ClassName = methodMapName,
220216
File = FileName
221217
});
222218
}
@@ -287,13 +283,13 @@ private int ConsumeSMMethodmap()
287283
}
288284
if (fStartIndex < fEndIndex)
289285
{
290-
fields.Add(new SMMethodmapField()
286+
fields.Add(new SMMethodmapField
291287
{
292288
Index = fStartIndex,
293289
Length = fEndIndex - fStartIndex + 1,
294290
Name = fieldName,
295291
File = FileName,
296-
MethodmapName = methodMapName,
292+
ClassName = methodMapName,
297293
FullName = source.Substring(fStartIndex, fEndIndex - fStartIndex + 1)
298294
});
299295
}
@@ -308,7 +304,6 @@ private int ConsumeSMMethodmap()
308304
Length = t[lastIndex].Index - startIndex + 1,
309305
Name = methodMapName,
310306
File = FileName,
311-
Type = methodMapType,
312307
InheritedType = inheriteType
313308
};
314309
mm.Methods.AddRange(methods);
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
namespace SourcepawnCondenser.SourcemodDefinition
1+
namespace SourcepawnCondenser.SourcemodDefinition;
2+
3+
abstract public class SMBaseDefinition
24
{
3-
public class SMBaseDefinition
4-
{
5-
public int Index = -1;
6-
public int Length = 0;
7-
public string File = string.Empty;
5+
public int Index = -1;
6+
public int Length = 0;
7+
public string File = string.Empty;
88

9-
public string Name = string.Empty;
10-
public string CommentString = string.Empty;
11-
}
9+
public string Name = string.Empty;
10+
public string CommentString = string.Empty;
1211
}

SourcepawnCondenser/SourcepawnCondenser/SourcemodDefinition/SMMethodmap.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33

44
namespace SourcepawnCondenser.SourcemodDefinition
55
{
6-
public class SMMethodmap : SMBaseDefinition
6+
public class SMMethodmap : SMClasslike
77
{
8-
public string Type = string.Empty;
98
public string InheritedType = string.Empty;
10-
public readonly List<SMMethodmapField> Fields = new();
11-
public readonly List<SMMethodmapMethod> Methods = new();
12-
9+
10+
1311
public List<ACNode> ProduceISNodes()
1412
{
1513
var nodes = new List<ACNode>();
@@ -24,19 +22,11 @@ public List<ACNode> ProduceISNodes()
2422
}
2523
}
2624

27-
public class SMMethodmapField : SMBaseDefinition
25+
public class SMMethodmapField : SMObjectField
2826
{
29-
public string MethodmapName = string.Empty;
30-
public string FullName = string.Empty;
31-
//public string Type = string.Empty; not needed yet
3227
}
3328

34-
public class SMMethodmapMethod : SMBaseDefinition
29+
public class SMMethodmapMethod : SMObjectMethod
3530
{
36-
public string MethodmapName = string.Empty;
37-
public string FullName = string.Empty;
38-
public string ReturnType = string.Empty;
39-
public string[] Parameters = new string[0];
40-
public string[] MethodKind = new string[0];
4131
}
4232
}

SourcepawnCondenser/SourcepawnCondenser/SourcepawnCondenser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
</ItemGroup>
6161
<ItemGroup>
6262
<Compile Include="Condenser.cs" />
63+
<Compile Include="CondenserFunctions\SMClasslike.cs" />
6364
<Compile Include="CondenserFunctions\SMEnumStructConsumer.cs" />
6465
<Compile Include="CondenserFunctions\SMPPDirectiveConsumer.cs" />
6566
<Compile Include="CondenserFunctions\SMConstantConsumer.cs" />

Spcode.sln.DotSettings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Classlike/@EntryIndexedValue">True</s:Boolean>
23
<s:Boolean x:Key="/Default/UserDictionary/Words/=endinput/@EntryIndexedValue">True</s:Boolean>
34
<s:Boolean x:Key="/Default/UserDictionary/Words/=endscript/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Methodmap/@EntryIndexedValue">True</s:Boolean>
46
<s:Boolean x:Key="/Default/UserDictionary/Words/=Methodmaps/@EntryIndexedValue">True</s:Boolean>
57
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sourcemod/@EntryIndexedValue">True</s:Boolean>
8+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sourcepawn/@EntryIndexedValue">True</s:Boolean>
69
<s:Boolean x:Key="/Default/UserDictionary/Words/=tryinclude/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

UI/Components/EditorElement/EditorElementIntellisenseController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ bool ComputeAutoComplete(string text, int lineOffset, int quoteCount)
395395
}
396396

397397

398+
if (text.Length == 0)
399+
return false;
400+
398401
if (!IsValidFunctionChar(text[lineOffset - 1]) &&
399402
text[lineOffset - 1] != '.' && text[lineOffset - 1] != ' ' && text[lineOffset - 1] != '\t')
400403
return false;

UI/Windows/SPDefinitionWindow.xaml.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ private void SPFunctionsListBox_SelectionChanged(object sender, SelectionChanged
169169
if (TagValue is SMMethodmap methodmap)
170170
{
171171
SPNameBlock.Text = methodmap.Name;
172-
SPFullNameBlock.Text = $"Type: " + methodmap.Type +
173-
$" - {Translate("InheritedFrom")}: {methodmap.InheritedType}";
172+
SPFullNameBlock.Text = $"{Translate("InheritedFrom")}: {methodmap.InheritedType}";
174173
SPFileBlock.Text = methodmap.File + ".inc" +
175174
$" ({string.Format(Translate("PosLen"), methodmap.Index, methodmap.Length)})";
176175
SPTypeBlock.Text = "Methodmap " + methodmap.Methods.Count + " methods - " + methodmap.Fields.Count + " fields";
@@ -198,7 +197,7 @@ private void SPFunctionsListBox_SelectionChanged(object sender, SelectionChanged
198197
SPFullNameBlock.Text = method.FullName;
199198
SPFileBlock.Text = method.File + ".inc" +
200199
$" {string.Format(Translate("PosLen"), method.Index, method.Length)}";
201-
SPTypeBlock.Text = $"{Translate("MethodFrom")} {method.MethodmapName}";
200+
SPTypeBlock.Text = $"{Translate("MethodFrom")} {method.ClassName}";
202201
SPCommentBox.Text = method.CommentString;
203202
return;
204203
}
@@ -209,7 +208,7 @@ private void SPFunctionsListBox_SelectionChanged(object sender, SelectionChanged
209208
SPFullNameBlock.Text = field.FullName;
210209
SPFileBlock.Text = field.File + ".inc" +
211210
$" {string.Format(Translate("PosLen"), field.Index, field.Length)}";
212-
SPTypeBlock.Text = $"{Translate("PropertyFrom")} {field.MethodmapName}";
211+
SPTypeBlock.Text = $"{Translate("PropertyFrom")} {field.ClassName}";
213212
SPCommentBox.Text = string.Empty;
214213
return;
215214
}

Utils/RegexKeywordsHelper.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ namespace SPCode.Utils
77
{
88
public static class RegexKeywordsHelper
99
{
10-
public static Regex GetRegexFromKeywords(string[] keywords, bool ForceAtomicRegex = false)
10+
/// <summary>
11+
/// Converts a string list a regexp matching all the given word.
12+
/// </summary>
13+
/// <param name="keywords">The words list</param>
14+
/// <param name="forceAtomicRegex">If true the word is enclosed in "\b"</param>
15+
/// <param name="noDot">If true the match requires a "." not to be present before the word</param>
16+
/// <returns></returns>
17+
public static Regex GetRegexFromKeywords(string[] keywords, bool forceAtomicRegex = false, bool noDot = false)
1118
{
12-
if (ForceAtomicRegex)
19+
if (forceAtomicRegex)
1320
{
1421
keywords = ConvertToAtomicRegexAbleStringArray(keywords);
1522
}
@@ -19,26 +26,10 @@ public static Regex GetRegexFromKeywords(string[] keywords, bool ForceAtomicRege
1926
return new Regex("SPEdit_Error"); //hehe
2027
}
2128

22-
var UseAtomicRegex = true;
23-
for (var j = 0; j < keywords.Length; ++j)
24-
{
25-
if (!char.IsLetterOrDigit(keywords[j][0]) ||
26-
!char.IsLetterOrDigit(keywords[j][keywords[j].Length - 1]))
27-
{
28-
UseAtomicRegex = false;
29-
break;
30-
}
31-
}
29+
var useAtomicRegex = keywords.All(t => char.IsLetterOrDigit(t[0]) && char.IsLetterOrDigit(t[t.Length - 1]));
3230

3331
var regexBuilder = new StringBuilder();
34-
if (UseAtomicRegex)
35-
{
36-
regexBuilder.Append(@"\b(?>");
37-
}
38-
else
39-
{
40-
regexBuilder.Append(@"(");
41-
}
32+
regexBuilder.Append(useAtomicRegex ? @"\b(?>" : @"(");
4233

4334
var orderedKeyWords = new List<string>(keywords);
4435
var i = 0;
@@ -49,7 +40,7 @@ public static Regex GetRegexFromKeywords(string[] keywords, bool ForceAtomicRege
4940
regexBuilder.Append('|');
5041
}
5142

52-
if (UseAtomicRegex)
43+
if (useAtomicRegex)
5344
{
5445
regexBuilder.Append(Regex.Escape(keyword));
5546
}
@@ -68,7 +59,7 @@ public static Regex GetRegexFromKeywords(string[] keywords, bool ForceAtomicRege
6859
}
6960
}
7061

71-
if (UseAtomicRegex)
62+
if (useAtomicRegex)
7263
{
7364
regexBuilder.Append(@")\b");
7465
}
@@ -159,16 +150,16 @@ public static Regex GetRegexFromKeywords2(string[] keywords)
159150
var regexBuilder = new StringBuilder(@"\b(?<=[^\s]+\.)(");
160151
regexBuilder.Append(string.Join("|", keywords));
161152
regexBuilder.Append(@")\b");
162-
153+
163154
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
164155
}
165-
156+
166157
public static Regex GetRegexFromKeywords2(List<string> keywords)
167158
{
168159
var regexBuilder = new StringBuilder(@"\b(?<=[^\s]+\.)(");
169160
regexBuilder.Append(string.Join("|", keywords));
170161
regexBuilder.Append(@")\b");
171-
162+
172163
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
173164
}
174165
}

0 commit comments

Comments
 (0)