Skip to content

Commit 89c80ac

Browse files
authored
Merge pull request #8 from wolfreak99/clean_up_code
Clean up code
2 parents e8b9052 + e151bb8 commit 89c80ac

File tree

3 files changed

+131
-93
lines changed

3 files changed

+131
-93
lines changed

Assets/UnityShell/Editor/Scripts/AutocompleteBox.cs

+49-31
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace UnityShell
77
[Serializable]
88
public class AutocompleteBox
99
{
10-
static class Styles
10+
private static class Styles
1111
{
1212
public const float resultHeight = 20f;
1313
public const float resultsBorderWidth = 2f;
@@ -36,7 +36,7 @@ static Styles()
3636
}
3737
}
3838

39-
const int HintToNextCompletionHeight = 7;
39+
private const int HintToNextCompletionHeight = 7;
4040

4141
public Action<string> onConfirm;
4242
public int maxResults = 10;
@@ -45,17 +45,17 @@ static Styles()
4545
public string[] results = new string[0];
4646

4747
[SerializeField]
48-
Vector2 scrollPos;
48+
private Vector2 scrollPos;
4949

5050
[SerializeField]
51-
int selectedIndex = -1;
51+
private int selectedIndex = -1;
5252

5353
[SerializeField]
54-
int visualIndex = -1;
54+
private int visualIndex = -1;
5555

56-
bool showResults;
56+
private bool showResults;
5757

58-
string searchString;
58+
private string searchString;
5959

6060
public void Clear()
6161
{
@@ -65,7 +65,10 @@ public void Clear()
6565

6666
public void OnGUI(string result, Rect rect)
6767
{
68-
if(results == null) results = new string[0];
68+
if (results == null)
69+
{
70+
results = new string[0];
71+
}
6972

7073
if (result != searchString)
7174
{
@@ -80,7 +83,10 @@ public void OnGUI(string result, Rect rect)
8083

8184
public void HandleEvents()
8285
{
83-
if(results.Length == 0) return;
86+
if (results.Length == 0)
87+
{
88+
return;
89+
}
8490

8591
var current = Event.current;
8692

@@ -100,20 +106,29 @@ public void HandleEvents()
100106
current.Use();
101107
selectedIndex++;
102108
}
103-
else if(current.keyCode == KeyCode.Return && selectedIndex >= 0)
109+
else if (current.keyCode == KeyCode.Return && selectedIndex >= 0)
104110
{
105111
current.Use();
106112
OnConfirm(results[selectedIndex]);
107113
}
108114

109-
if (selectedIndex >= results.Length) selectedIndex = 0;
110-
else if (selectedIndex < 0) selectedIndex = results.Length - 1;
115+
if (selectedIndex >= results.Length)
116+
{
117+
selectedIndex = 0;
118+
}
119+
else if (selectedIndex < 0)
120+
{
121+
selectedIndex = results.Length - 1;
122+
}
111123
}
112124
}
113125

114-
void DoResults(Rect drawRect)
126+
private void DoResults(Rect drawRect)
115127
{
116-
if(results.Length <= 0 || !showResults) return;
128+
if (results.Length <= 0 || !showResults)
129+
{
130+
return;
131+
}
117132

118133
var current = Event.current;
119134
drawRect.height = Styles.resultHeight * Mathf.Min(maxResults, results.Length);
@@ -123,7 +138,7 @@ void DoResults(Rect drawRect)
123138
drawRect.height += Styles.resultsBorderWidth;
124139

125140
var backgroundRect = drawRect;
126-
if(results.Length > maxResults)
141+
if (results.Length > maxResults)
127142
{
128143
backgroundRect.height += HintToNextCompletionHeight + Styles.resultsBorderWidth;
129144
}
@@ -154,21 +169,21 @@ void DoResults(Rect drawRect)
154169
elementRect.x = Styles.resultsBorderWidth;
155170
elementRect.y = 0;
156171

157-
if(results.Length > maxResults)
172+
if (results.Length > maxResults)
158173
{
159174
elementRect.y = -visualIndex * Styles.resultHeight;
160175

161176
var maxPos = GetTotalResultsShown(clipRect) * Styles.resultHeight - HintToNextCompletionHeight;
162177

163-
if(-elementRect.y > maxPos)
178+
if (-elementRect.y > maxPos)
164179
{
165180
elementRect.y = -maxPos;
166181
}
167182
}
168183

169184
for (var i = 0; i < results.Length; i++)
170185
{
171-
if(current.type == EventType.Repaint)
186+
if (current.type == EventType.Repaint)
172187
{
173188
var style = i % 2 == 0 ? Styles.entryOdd : Styles.entryEven;
174189

@@ -181,30 +196,30 @@ void DoResults(Rect drawRect)
181196
elementRect.y += Styles.resultHeight;
182197
}
183198

184-
if(results.Length > maxResults)
199+
if (results.Length > maxResults)
185200
{
186201
DrawScroll(clipRect);
187202
}
188203
}
189204
GUI.EndClip();
190205
}
191206

192-
void DrawScroll(Rect clipRect)
207+
private void DrawScroll(Rect clipRect)
193208
{
194209
var scrollRect = clipRect;
195210
scrollRect.x += scrollRect.width - 30;
196211
scrollRect.y = 0;
197212

198213
var resultsShown = GetTotalResultsShown(clipRect);
199214

200-
scrollRect.height = ((float) maxResults / resultsShown * clipRect.height);
215+
scrollRect.height = ((float) maxResults / resultsShown * clipRect.height);
201216

202217
scrollRect.y = ((float) visualIndex / (resultsShown)) * (clipRect.height - scrollRect.height);
203218

204219
GUI.Box(scrollRect, GUIContent.none, Styles.sliderStyle);
205220
}
206221

207-
int GetTotalResultsShown(Rect clipRect)
222+
private int GetTotalResultsShown(Rect clipRect)
208223
{
209224
// Actual scrolling is a bit less as there's also the view itself in which is not scrolled,
210225
// when moving down initially, for example.
@@ -213,7 +228,7 @@ int GetTotalResultsShown(Rect clipRect)
213228
return resultsShown;
214229
}
215230

216-
void UpdateVisualIndex(Rect clipRect)
231+
private void UpdateVisualIndex(Rect clipRect)
217232
{
218233
var ySelectedPos = selectedIndex * Styles.resultHeight;
219234
var yVisualPos = visualIndex * Styles.resultHeight;
@@ -226,27 +241,30 @@ void UpdateVisualIndex(Rect clipRect)
226241
var diffMax = ySelectedPos - (yVisualPos + max) + Styles.resultHeight;
227242
var diffMin = (yVisualPos + min) - ySelectedPos;
228243

229-
if(diffMax > 0)
244+
if (diffMax > 0)
230245
{
231-
visualIndex += Mathf.CeilToInt(diffMax / Styles.resultHeight);
246+
visualIndex += Mathf.CeilToInt(diffMax / Styles.resultHeight);
232247
}
233-
else if(diffMin > 0)
248+
else if (diffMin > 0)
234249
{
235-
visualIndex -= Mathf.CeilToInt(diffMin / Styles.resultHeight);
250+
visualIndex -= Mathf.CeilToInt(diffMin / Styles.resultHeight);
236251
}
237252
}
238253

239-
void OnConfirm(string result)
254+
private void OnConfirm(string result)
240255
{
241-
if(onConfirm != null) onConfirm(result);
256+
if (onConfirm != null)
257+
{
258+
onConfirm(result);
259+
}
242260
RepaintFocusedWindow();
243261
showResults = false;
244262
searchString = result;
245263
}
246264

247-
static void RepaintFocusedWindow()
265+
private static void RepaintFocusedWindow()
248266
{
249-
if(EditorWindow.focusedWindow != null)
267+
if (EditorWindow.focusedWindow != null)
250268
{
251269
EditorWindow.focusedWindow.Repaint();
252270
}

Assets/UnityShell/Editor/Scripts/ShellEvaluator.cs

+23-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ShellEvaluator
1010
{
1111
public string[] completions;
1212

13-
int handleCount;
13+
private int handleCount;
1414

1515
#if NET_4_6 || NET_STANDARD_2_0
1616
public Evaluator evaluator;
@@ -25,11 +25,23 @@ private void InitializeEvaluator()
2525
{
2626
#if NET_4_6 || NET_STANDARD_2_0
2727
evaluator = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()));
28-
AppDomain.CurrentDomain.GetAssemblies().ToList().ForEach(asm => {try {evaluator.ReferenceAssembly(asm);}catch{}});
28+
AppDomain.CurrentDomain.GetAssemblies().ToList().ForEach(asm => {
29+
try
30+
{
31+
evaluator.ReferenceAssembly(asm);
32+
}
33+
catch { }
34+
});
2935
evaluator.Run("using UnityEngine; using UnityEditor; using System; using System.Collections.Generic;");
3036
#else
31-
AppDomain.CurrentDomain.GetAssemblies().ToList().ForEach(asm => {try {Evaluator.ReferenceAssembly(asm);}catch{}});
32-
Evaluator.Run ("using UnityEngine; using UnityEditor; using System; using System.Collections.Generic;");
37+
AppDomain.CurrentDomain.GetAssemblies().ToList().ForEach(asm => {
38+
try
39+
{
40+
Evaluator.ReferenceAssembly(asm);
41+
}
42+
catch { }
43+
});
44+
Evaluator.Run("using UnityEngine; using UnityEditor; using System; using System.Collections.Generic;");
3345
#endif
3446
}
3547

@@ -47,7 +59,7 @@ public void SetInput(string input)
4759
{
4860
int handle = handleCount;
4961

50-
if(!string.IsNullOrEmpty(input))
62+
if (!string.IsNullOrEmpty(input))
5163
{
5264
string prefix;
5365
#if NET_4_6 || NET_STANDARD_2_0
@@ -57,7 +69,7 @@ public void SetInput(string input)
5769
#endif
5870

5971
// Avoid old threads overriding with old results
60-
if(handle == handleCount)
72+
if (handle == handleCount)
6173
{
6274
completions = result;
6375
if (completions == null)
@@ -69,7 +81,7 @@ public void SetInput(string input)
6981
completions[i] = input + completions[i];
7082
}
7183

72-
if(completions.Length == 1 && completions[0].Trim() == input.Trim())
84+
if (completions.Length == 1 && completions[0].Trim() == input.Trim())
7385
{
7486
completions = new string[0];
7587
}
@@ -91,7 +103,7 @@ public object Evaluate(string command)
91103
}
92104
#endif
93105

94-
if(!command.EndsWith(";"))
106+
if (!command.EndsWith(";"))
95107
{
96108
command += ";";
97109
}
@@ -110,8 +122,10 @@ public object Evaluate(string command)
110122
compilationResult(ref result);
111123

112124
if (result == null)
125+
{
113126
result = "Executed code successfully";
127+
}
114128
return result;
115129
}
116130
}
117-
}
131+
}

0 commit comments

Comments
 (0)