Skip to content

Commit 41ab19c

Browse files
author
SlavaRa
committed
Improves "extract locale variable" when working with generic types
1 parent 1a25740 commit 41ab19c

File tree

10 files changed

+105
-227
lines changed

10 files changed

+105
-227
lines changed

External/Plugins/ASCompletion/Completion/ASGenerator.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,13 +2747,11 @@ private static void GenerateClass(ScintillaControl sci, String className, ClassM
27472747

27482748
public static void GenerateExtractVariable(ScintillaControl Sci, string NewName)
27492749
{
2750-
FileModel cFile;
2751-
27522750
string expression = Sci.SelText.Trim(new char[] { '=', ' ', '\t', '\n', '\r', ';', '.' });
27532751
expression = expression.TrimEnd(new char[] { '(', '[', '{', '<' });
27542752
expression = expression.TrimStart(new char[] { ')', ']', '}', '>' });
27552753

2756-
cFile = ASContext.Context.CurrentModel;
2754+
var cFile = ASContext.Context.CurrentModel;
27572755
ASFileParser parser = new ASFileParser();
27582756
parser.ParseSrc(cFile, Sci.Text);
27592757

@@ -2764,18 +2762,19 @@ public static void GenerateExtractVariable(ScintillaControl Sci, string NewName)
27642762
int funcBodyStart = GetBodyStart(current.LineFrom, current.LineTo, Sci);
27652763
Sci.SetSel(funcBodyStart, Sci.LineEndPosition(current.LineTo));
27662764
string currentMethodBody = Sci.SelText;
2765+
var insertPosition = funcBodyStart + currentMethodBody.IndexOfOrdinal(expression);
2766+
var line = Sci.LineFromPosition(insertPosition);
2767+
insertPosition = Sci.LineIndentPosition(line);
27672768

2768-
bool isExprInSingleQuotes = (expression.StartsWith('\'') && expression.EndsWith('\''));
2769-
bool isExprInDoubleQuotes = (expression.StartsWith('\"') && expression.EndsWith('\"'));
27702769
int stylemask = (1 << Sci.StyleBits) - 1;
27712770
int lastPos = -1;
2772-
char prevOrNextChar;
27732771
Sci.Colourise(0, -1);
27742772
while (true)
27752773
{
27762774
lastPos = currentMethodBody.IndexOfOrdinal(expression, lastPos + 1);
27772775
if (lastPos > -1)
27782776
{
2777+
char prevOrNextChar;
27792778
if (lastPos > 0)
27802779
{
27812780
prevOrNextChar = currentMethodBody[lastPos - 1];
@@ -2793,22 +2792,10 @@ public static void GenerateExtractVariable(ScintillaControl Sci, string NewName)
27932792
}
27942793
}
27952794

2796-
int style = Sci.StyleAt(funcBodyStart + lastPos) & stylemask;
2797-
if (ASComplete.IsCommentStyle(style))
2798-
{
2799-
continue;
2800-
}
2801-
else if ((isExprInDoubleQuotes && currentMethodBody[lastPos] == '"' && currentMethodBody[lastPos + expression.Length - 1] == '"')
2802-
|| (isExprInSingleQuotes && currentMethodBody[lastPos] == '\'' && currentMethodBody[lastPos + expression.Length - 1] == '\''))
2803-
{
2804-
2805-
}
2806-
else if (!ASComplete.IsTextStyle(style))
2807-
{
2808-
continue;
2809-
}
2810-
2811-
Sci.SetSel(funcBodyStart + lastPos, funcBodyStart + lastPos + expression.Length);
2795+
var pos = funcBodyStart + lastPos;
2796+
int style = Sci.StyleAt(pos) & stylemask;
2797+
if (ASComplete.IsCommentStyle(style)) continue;
2798+
Sci.SetSel(pos, pos + expression.Length);
28122799
Sci.ReplaceSel(NewName);
28132800
currentMethodBody = currentMethodBody.Substring(0, lastPos) + NewName + currentMethodBody.Substring(lastPos + expression.Length);
28142801
lastPos += NewName.Length;
@@ -2818,8 +2805,7 @@ public static void GenerateExtractVariable(ScintillaControl Sci, string NewName)
28182805
break;
28192806
}
28202807
}
2821-
2822-
Sci.CurrentPos = funcBodyStart;
2808+
Sci.CurrentPos = insertPosition;
28232809
Sci.SetSel(Sci.CurrentPos, Sci.CurrentPos);
28242810

28252811
MemberModel m = new MemberModel(NewName, "", FlagType.LocalVar, 0);

External/Plugins/CodeRefactor/Commands/ExtractLocalVariableCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace CodeRefactor.Commands
66
{
77
class ExtractLocalVariableCommand
88
{
9-
private string NewName;
9+
private readonly string NewName;
1010

1111
public ExtractLocalVariableCommand(string newName)
1212
{
@@ -15,15 +15,15 @@ public ExtractLocalVariableCommand(string newName)
1515

1616
public void Execute()
1717
{
18-
ScintillaControl Sci = PluginBase.MainForm.CurrentDocument.SciControl;
19-
Sci.BeginUndoAction();
18+
ScintillaControl sci = PluginBase.MainForm.CurrentDocument.SciControl;
19+
sci.BeginUndoAction();
2020
try
2121
{
22-
ASGenerator.GenerateExtractVariable(Sci, NewName);
22+
ASGenerator.GenerateExtractVariable(sci, NewName);
2323
}
2424
finally
2525
{
26-
Sci.EndUndoAction();
26+
sci.EndUndoAction();
2727
}
2828
}
2929
}

Tests/External/Plugins/ASCompletion.Tests/ASCompletion.Tests.csproj

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,9 @@
106106
<SubType>UserControl</SubType>
107107
</Compile>
108108
<Compile Include="Properties\AssemblyInfo.cs" />
109-
<Compile Include="Properties\Resources.Designer.cs">
110-
<AutoGen>True</AutoGen>
111-
<DesignTime>True</DesignTime>
112-
<DependentUpon>Resources.resx</DependentUpon>
113-
</Compile>
114109
<EmbeddedResource Include="Test Files\parser\haxe\WrongSyntaxCompilerMetaAfterMethodWithNoType.hx" />
110+
<EmbeddedResource Include="Test Files\generated\haxe\BeforeGenerateExtractVariableGeneric.hx" />
111+
<EmbeddedResource Include="Test Files\generated\haxe\AfterGenerateExtractVariableGeneric.hx" />
115112
<Compile Include="TestUtils\ContextExtensions.cs" />
116113
<Compile Include="TestUtils\TestFile.cs" />
117114
</ItemGroup>
@@ -213,12 +210,6 @@
213210
<Install>true</Install>
214211
</BootstrapperPackage>
215212
</ItemGroup>
216-
<ItemGroup>
217-
<EmbeddedResource Include="Properties\Resources.resx">
218-
<Generator>ResXFileCodeGenerator</Generator>
219-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
220-
</EmbeddedResource>
221-
</ItemGroup>
222213
<ItemGroup>
223214
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
224215
</ItemGroup>

Tests/External/Plugins/ASCompletion.Tests/Completion/ASGeneratorTests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// TODO: Tests with different formatting options using parameterized tests
22

3+
using System;
34
using System.Collections;
45
using System.Collections.Generic;
5-
using AS3Context;
6+
using System.Diagnostics;
67
using ASCompletion.Context;
78
using ASCompletion.Model;
89
using ASCompletion.Settings;
@@ -459,6 +460,46 @@ public string Haxe(string sourceText, ClassModel sourceModel, ClassModel interfa
459460
return sci.Text;
460461
}
461462
}
463+
464+
[TestFixture]
465+
public class GenerateExtractVariable : GenerateJob
466+
{
467+
public IEnumerable<TestCaseData> GenerateExtractVariableHaxeTestCases
468+
{
469+
get
470+
{
471+
yield return
472+
new TestCaseData(
473+
TestFile.ReadAllText(
474+
"ASCompletion.Test_Files.generated.haxe.BeforeGenerateExtractVariableGeneric.hx"),
475+
new MemberModel("main", null, FlagType.Static | FlagType.Function, 0)
476+
{
477+
LineFrom = 2,
478+
LineTo = 4
479+
},
480+
"new Test<String>(\"test\").get()",
481+
"s"
482+
)
483+
.Returns(
484+
TestFile.ReadAllText(
485+
"ASCompletion.Test_Files.generated.haxe.AfterGenerateExtractVariableGeneric.hx"))
486+
.SetName("GenerateExtractVariable");
487+
}
488+
}
489+
490+
[Test, TestCaseSource("GenerateExtractVariableHaxeTestCases")]
491+
public string Haxe(string sourceText, MemberModel sourceModel, string selectText, string newName)
492+
{
493+
ASContext.Context.SetHaxeFeatures();
494+
ASContext.Context.CurrentModel.Returns(new FileModel {haXe = true, Context = ASContext.Context});
495+
ASContext.Context.CurrentMember.Returns(sourceModel);
496+
sci.Text = sourceText;
497+
sci.ConfigurationLanguage = "haxe";
498+
sci.SelectText(selectText, 0);
499+
ASGenerator.GenerateExtractVariable(sci, newName);
500+
return sci.Text;
501+
}
502+
}
462503
}
463504
}
464505
}

Tests/External/Plugins/ASCompletion.Tests/MainForm.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Drawing;
44
using System.Windows.Forms;
5+
using FlashDevelop.Dialogs;
56
using FlashDevelop.Utilities;
67
using PluginCore;
78
using ScintillaNet.Configuration;
@@ -26,6 +27,11 @@ public void RefreshSciConfig()
2627
throw new NotImplementedException();
2728
}
2829

30+
public void RestartRequired()
31+
{
32+
throw new NotImplementedException();
33+
}
34+
2935
public void ThemeControls(object control)
3036
{
3137
// Not implemented
@@ -143,6 +149,7 @@ public Dictionary<Keys, string> GetShortcutItemsByKeys()
143149

144150
public string GetThemeValue(string id)
145151
{
152+
if (id == "ScrollBar.UseCustom") return string.Empty;
146153
throw new NotImplementedException();
147154
}
148155

@@ -228,6 +235,11 @@ public Image FindImageAndSetAdjust(string data)
228235
throw new NotImplementedException();
229236
}
230237

238+
public int GetInstanceCount()
239+
{
240+
throw new NotImplementedException();
241+
}
242+
231243
public ISettings Settings { get; set; }
232244

233245
public ToolStrip ToolStrip

Tests/External/Plugins/ASCompletion.Tests/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
3+
using PluginCore;
44

55
// General Information about an assembly is controlled through the following
66
// set of attributes. Change these attribute values to modify the information
77
// associated with an assembly.
88
[assembly: AssemblyTitle("ASCompletion.Tests")]
99
[assembly: AssemblyDescription("")]
1010
[assembly: AssemblyConfiguration("")]
11-
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyCompany(DistroConfig.DISTRIBUTION_COMPANY)]
1212
[assembly: AssemblyProduct("ASCompletion.Tests")]
13-
[assembly: AssemblyCopyright("Copyright © 2015")]
13+
[assembly: AssemblyCopyright(DistroConfig.DISTRIBUTION_COPYRIGHT)]
1414
[assembly: AssemblyTrademark("")]
1515
[assembly: AssemblyCulture("")]
1616

Tests/External/Plugins/ASCompletion.Tests/Properties/Resources.Designer.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)