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

Commit 556be2d

Browse files
committed
Pseudo implementation of local variables intellisense
1 parent bf8cfea commit 556be2d

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

SourcepawnCondenser/SourcepawnCondenser/Condenser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public partial class Condenser
99
{
1010
private readonly SMDefinition def;
1111

12-
private readonly string FileName = string.Empty;
12+
private readonly string FileName;
1313
private readonly int length;
14-
private readonly string source = string.Empty;
14+
private readonly string source;
1515
private readonly Token[] t;
1616
private int position;
1717

@@ -141,6 +141,8 @@ public SMDefinition Condense()
141141
position = newIndex + 1;
142142
continue;
143143
}
144+
145+
// If Variable is not found try function
144146
newIndex = ConsumeSMFunction();
145147
if (newIndex != -1)
146148
{

SourcepawnCondenser/SourcepawnCondenser/CondenserFunctions/SMFunctionConsumer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ private int ConsumeSMFunction()
197197
{
198198
FunctionKind = kind,
199199
Index = t[startPosition].Index,
200+
EndPos = parameterDeclIndexEnd,
200201
File = FileName,
201202
Length = parameterDeclIndexEnd - t[startPosition].Index + 1,
202203
Name = functionName,
@@ -228,6 +229,7 @@ private int ConsumeSMFunction()
228229
localVars = LocalVars.ConsumeSMVariableLocal(segment.ToArray(), FileName);
229230
def.Functions.Add(new SMFunction
230231
{
232+
EndPos = t[nextOpenBraceTokenIndex + (i - nextOpenBraceTokenIndex) + 1].Index,
231233
FunctionKind = kind,
232234
Index = t[startPosition].Index,
233235
File = FileName,
@@ -248,6 +250,7 @@ private int ConsumeSMFunction()
248250

249251
def.Functions.Add(new SMFunction
250252
{
253+
EndPos = parameterDeclIndexEnd,
251254
FunctionKind = kind,
252255
Index = t[startPosition].Index,
253256
File = FileName,

SourcepawnCondenser/SourcepawnCondenser/SMDefinition/SMDefinition.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public string[]
3939
public List<SMTypedef> Typedefs = new List<SMTypedef>();
4040
public string[] TypeStrings = new string[0];
4141
public List<SMVariable> Variables = new List<SMVariable>();
42-
public string[] VariableStrings = new string[0];
43-
42+
4443
public void Sort()
4544
{
4645
try
@@ -90,7 +89,7 @@ public void AppendFiles(IEnumerable<string> paths)
9089
ProduceStringArrays();
9190
}
9291

93-
private void ProduceStringArrays()
92+
private void ProduceStringArrays(int caret = -1, string text = "")
9493
{
9594
FunctionStrings = new string[Functions.Count];
9695
for (var i = 0; i < Functions.Count; ++i) FunctionStrings[i] = Functions[i].Name;
@@ -100,7 +99,6 @@ private void ProduceStringArrays()
10099
var enumStructNames = new List<string>();
101100
var structMethodNames = new List<string>();
102101
var structFieldNames = new List<string>();
103-
var varNames = new List<string>();
104102

105103
foreach (var mm in Methodmaps)
106104
{
@@ -116,22 +114,29 @@ private void ProduceStringArrays()
116114
structFieldNames.AddRange(sm.Fields.Select(f => f.Name));
117115
}
118116

119-
foreach (var v in Variables) varNames.Add(v.Name);
120-
121117
MethodsStrings = methodNames.ToArray();
122118
FieldStrings = fieldNames.ToArray();
123119
StructFieldStrings = structFieldNames.ToArray();
124120
StructMethodStrings = structMethodNames.ToArray();
125121
MethodmapsStrings = methodmapNames.ToArray();
126122
EnumStructStrings = enumStructNames.ToArray();
127-
VariableStrings = varNames.ToArray();
128123

129124
var constantNames = Constants.Select(i => i.Name).ToList();
130125
foreach (var e in Enums) constantNames.AddRange(e.Entries);
131126

132127
constantNames.AddRange(Defines.Select(i => i.Name));
133128
constantNames.AddRange(Variables.Select(v => v.Name));
134129

130+
if (caret != -1)
131+
{
132+
var currentFunc = Functions.FirstOrDefault(e => e.Index < caret && caret <= e.EndPos && e.File.EndsWith(".sp"));
133+
if (currentFunc != null)
134+
{
135+
136+
constantNames.AddRange(currentFunc.FuncVariables.Select(v => v.Name));
137+
}
138+
}
139+
135140
constantNames.Sort(string.Compare);
136141
ConstantsStrings = constantNames.ToArray();
137142
var typeNames = new List<string>
@@ -171,7 +176,7 @@ public ISNode[] ProduceISNodes()
171176
nodes.AddRange(ISNode.ConvertFromStringArray(FieldStrings, false, "• "));
172177
nodes.AddRange(ISNode.ConvertFromStringArray(StructFieldStrings, false, "• "));
173178

174-
nodes.AddRange(ISNode.ConvertFromStringArray(VariableStrings, false, "v "));
179+
// nodes.AddRange(ISNode.ConvertFromStringArray(VariableStrings, false, "v "));
175180

176181
nodes = nodes.Distinct(new ISNodeEqualityComparer()).ToList();
177182
nodes.Sort((a, b) => string.CompareOrdinal(a.EntryName, b.EntryName));
@@ -198,30 +203,24 @@ public void MergeDefinitions(SMDefinition def)
198203
}
199204
}
200205

201-
public SMDefinition ProduceTemporaryExpandedDefinition(SMDefinition[] definitions)
206+
public SMDefinition ProduceTemporaryExpandedDefinition(SMDefinition[] definitions, int caret, string text)
202207
{
203208
var def = new SMDefinition();
204-
try
205-
{
206209
def.MergeDefinitions(this);
207-
for (var i = 0; i < definitions.Length; ++i)
208-
if (definitions[i] != null)
209-
def.MergeDefinitions(definitions[i]);
210-
def.Sort();
211-
def.ProduceStringArrays();
212-
}
213-
catch (Exception)
214-
{
215-
}
210+
foreach (var definition in definitions)
211+
if (definition != null)
212+
def.MergeDefinitions(definition);
216213

214+
def.Sort();
215+
def.ProduceStringArrays(caret, text);
217216
return def;
218217
}
219218

220219
private class SMFunctionComparer : IEqualityComparer<SMFunction>
221220
{
222221
public bool Equals(SMFunction left, SMFunction right)
223222
{
224-
return left.Name == right.Name;
223+
return left?.Name == right?.Name;
225224
}
226225

227226
public int GetHashCode(SMFunction sm)
@@ -234,7 +233,7 @@ private class SMEnumComparer : IEqualityComparer<SMEnum>
234233
{
235234
public bool Equals(SMEnum left, SMEnum right)
236235
{
237-
return left.Name == right.Name;
236+
return left?.Name == right?.Name;
238237
}
239238

240239
public int GetHashCode(SMEnum sm)
@@ -247,7 +246,7 @@ private class SMStructComparer : IEqualityComparer<SMStruct>
247246
{
248247
public bool Equals(SMStruct left, SMStruct right)
249248
{
250-
return left.Name == right.Name;
249+
return left?.Name == right?.Name;
251250
}
252251

253252
public int GetHashCode(SMStruct sm)
@@ -260,7 +259,7 @@ private class SMDefineComparer : IEqualityComparer<SMDefine>
260259
{
261260
public bool Equals(SMDefine left, SMDefine right)
262261
{
263-
return left.Name == right.Name;
262+
return left?.Name == right?.Name;
264263
}
265264

266265
public int GetHashCode(SMDefine sm)
@@ -273,7 +272,7 @@ private class SMConstantComparer : IEqualityComparer<SMConstant>
273272
{
274273
public bool Equals(SMConstant left, SMConstant right)
275274
{
276-
return left.Name == right.Name;
275+
return left?.Name == right?.Name;
277276
}
278277

279278
public int GetHashCode(SMConstant sm)
@@ -286,7 +285,7 @@ private class SMMethodmapsComparer : IEqualityComparer<SMMethodmap>
286285
{
287286
public bool Equals(SMMethodmap left, SMMethodmap right)
288287
{
289-
return left.Name == right.Name;
288+
return left?.Name == right?.Name;
290289
}
291290

292291
public int GetHashCode(SMMethodmap sm)
@@ -307,7 +306,7 @@ public class ISNodeEqualityComparer : IEqualityComparer<ISNode>
307306
{
308307
public bool Equals(ISNode nodeA, ISNode nodeB)
309308
{
310-
return nodeA.EntryName == nodeB.EntryName;
309+
return nodeA?.EntryName == nodeB?.EntryName;
311310
}
312311

313312
public int GetHashCode(ISNode node)

SourcepawnCondenser/SourcepawnCondenser/SMDefinition/SMFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class SMFunction
66
{
77
public int Index = -1;
88
public int Length = 0;
9+
public int EndPos = -1;
910
public string File = string.Empty;
1011

1112
public string Name = string.Empty;
@@ -15,7 +16,6 @@ public class SMFunction
1516
public string[] Parameters = new string[0];
1617
public SMFunctionKind FunctionKind = SMFunctionKind.Unknown;
1718
public List<SMVariable> FuncVariables = new List<SMVariable>();
18-
public string Body = string.Empty;
1919
}
2020

2121
public enum SMFunctionKind

UI/MainWindowBackgroundParser.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ private void ParseDistributorTimer_Elapsed(object sender, ElapsedEventArgs args)
4343
if (ee == null || ce == null) return;
4444

4545
Debug.Assert(ee != null, nameof(ee) + " != null");
46+
// ReSharper disable once PossibleNullReferenceException
4647
foreach (var e in ee)
4748
if (e.LastSMDefUpdateUID < currentSMDefUID) //wants an update of the SMDefinition
4849
{
4950
if (e == ce)
5051
{
5152
Debug.Assert(ce != null, nameof(ce) + " != null");
53+
// ReSharper disable once PossibleNullReferenceException
5254
if (ce.ISAC_Open) continue;
5355
}
5456

@@ -63,8 +65,11 @@ private void BackgroundParser_Worker()
6365
while (true)
6466
while (Program.OptionsObject.Program_DynamicISAC)
6567
{
66-
Thread.Sleep(5000);
68+
Thread.Sleep(1000);
6769
var ee = GetAllEditorElements();
70+
var caret = -1;
71+
var text = string.Empty;
72+
6873
if (ee != null)
6974
{
7075
var definitions = new SMDefinition[ee.Length];
@@ -81,15 +86,19 @@ private void BackgroundParser_Worker()
8186
Dispatcher.Invoke(() =>
8287
{
8388
if (ee[i1].IsLoaded)
89+
{
90+
caret = ee[i1].editor.CaretOffset;
91+
text = ee[i1].editor.Text;
8492
definitions[i1] =
8593
new Condenser(File.ReadAllText(fInfo.FullName), fInfo.Name)
8694
.Condense();
95+
}
8796
});
8897
}
8998
}
9099

91100
currentSMDef = Program.Configs[Program.SelectedConfig].GetSMDef()
92-
.ProduceTemporaryExpandedDefinition(definitions);
101+
.ProduceTemporaryExpandedDefinition(definitions, caret, text);
93102
currentSMFunctions = currentSMDef.Functions.ToArray();
94103
currentACNodes = currentSMDef.ProduceACNodes();
95104
currentISNodes = currentSMDef.ProduceISNodes();

0 commit comments

Comments
 (0)