Skip to content

Commit f0ba0d4

Browse files
committed
Merge pull request #950 from wise0704/CodeRefactor
Include available Refactor menus in Contextual Code Generation list
2 parents 9e1bdec + ab50990 commit f0ba0d4

File tree

13 files changed

+248
-177
lines changed

13 files changed

+248
-177
lines changed

External/Plugins/AS2Context/Context.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ public Context(AS2Settings initSettings)
9999
features.objectKey = "Object";
100100
features.booleanKey = "Boolean";
101101
features.numberKey = "Number";
102+
features.stringKey = "String";
102103
features.arrayKey = "Array";
104+
features.dynamicKey = "*";
103105
features.importKey = "import";
104106
features.typesPreKeys = new string[] { "import", "new", "instanceof", "extends", "implements" };
105107
features.codeKeywords = new string[] {

External/Plugins/AS3Context/Context.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public Context(AS3Settings initSettings)
101101
features.objectKey = "Object";
102102
features.booleanKey = "Boolean";
103103
features.numberKey = "Number";
104+
features.stringKey = "String";
104105
features.arrayKey = "Array";
106+
features.dynamicKey = "*";
105107
features.importKey = "import";
106108
features.typesPreKeys = new string[] { "import", "new", "typeof", "is", "as", "extends", "implements" };
107109
features.codeKeywords = new string[] {

External/Plugins/ASCompletion/Completion/ASComplete.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,8 @@ private static void ClearResolvedContext()
862862

863863
private static void NotifyContextChanged()
864864
{
865-
if (OnResolvedContextChanged != null)
866-
OnResolvedContextChanged(CurrentResolvedContext);
865+
if (OnResolvedContextChanged != null)
866+
OnResolvedContextChanged.Invoke(CurrentResolvedContext);
867867
}
868868

869869
/// <summary>

External/Plugins/ASCompletion/Completion/ASGenerator.cs

Lines changed: 57 additions & 86 deletions
Large diffs are not rendered by default.

External/Plugins/ASCompletion/Completion/ContextFeatures.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public class ContextFeatures
7272
public string objectKey;
7373
public string booleanKey;
7474
public string numberKey;
75+
public string stringKey;
7576
public string arrayKey;
77+
public string dynamicKey;
7678
public string importKey;
7779
public string importKeyAlt;
7880
public string[] typesPreKeys = new string[] { };

External/Plugins/ASCompletion/PluginMain.cs

Lines changed: 89 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -446,104 +446,108 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
446446
// Actionscript context specific
447447
//
448448
if (ASContext.Context.IsFileValid)
449-
switch (e.Type)
450-
{
451-
case EventType.ProcessArgs:
452-
TextEvent te = (TextEvent)e;
453-
if (reArgs.IsMatch(te.Value))
454-
{
455-
// resolve current element
456-
Hashtable details = ASComplete.ResolveElement(sci, null);
457-
te.Value = ArgumentsProcessor.Process(te.Value, details);
458-
459-
if (te.Value.IndexOf("$") >= 0 && reCostlyArgs.IsMatch(te.Value))
449+
switch (e.Type)
450+
{
451+
case EventType.ProcessArgs:
452+
TextEvent te = (TextEvent) e;
453+
if (reArgs.IsMatch(te.Value))
460454
{
461-
ASResult result = ASComplete.CurrentResolvedContext.Result ?? new ASResult();
462-
details = new Hashtable();
463-
// Get closest list (Array or Vector)
464-
string closestListName = "", closestListItemType = "";
465-
ASComplete.FindClosestList(ASContext.Context, result.Context, sci.CurrentLine, ref closestListName, ref closestListItemType);
466-
details.Add("TypClosestListName", closestListName);
467-
details.Add("TypClosestListItemType", closestListItemType);
468-
// get free iterator index
469-
string iterator = ASComplete.FindFreeIterator(ASContext.Context, ASContext.Context.CurrentClass, result.Context);
470-
details.Add("ItmUniqueVar", iterator);
455+
// resolve current element
456+
Hashtable details = ASComplete.ResolveElement(sci, null);
471457
te.Value = ArgumentsProcessor.Process(te.Value, details);
472-
}
473-
}
474-
break;
475458

476-
// menu commands
477-
case EventType.Command:
478-
string command = (e as DataEvent).Action ?? "";
479-
if (command.StartsWith("ASCompletion."))
480-
{
481-
string cmdData = (e as DataEvent).Data as string;
482-
// run MTASC
483-
if (command == "ASCompletion.CustomBuild")
484-
{
485-
if (cmdData != null) ASContext.Context.RunCMD(cmdData);
486-
else ASContext.Context.RunCMD("");
487-
e.Handled = true;
459+
if (te.Value.IndexOf("$") >= 0 && reCostlyArgs.IsMatch(te.Value))
460+
{
461+
ASResult result = ASComplete.CurrentResolvedContext.Result ?? new ASResult();
462+
details = new Hashtable();
463+
// Get closest list (Array or Vector)
464+
string closestListName = "", closestListItemType = "";
465+
ASComplete.FindClosestList(ASContext.Context, result.Context, sci.CurrentLine, ref closestListName, ref closestListItemType);
466+
details.Add("TypClosestListName", closestListName);
467+
details.Add("TypClosestListItemType", closestListItemType);
468+
// get free iterator index
469+
string iterator = ASComplete.FindFreeIterator(ASContext.Context, ASContext.Context.CurrentClass, result.Context);
470+
details.Add("ItmUniqueVar", iterator);
471+
te.Value = ArgumentsProcessor.Process(te.Value, details);
472+
}
488473
}
474+
break;
489475

490-
// build the SWF using MTASC
491-
else if (command == "ASCompletion.QuickBuild")
476+
// menu commands
477+
case EventType.Command:
478+
de = e as DataEvent;
479+
string command = de.Action ?? "";
480+
if (command.StartsWith("ASCompletion.", StringComparison.Ordinal))
492481
{
493-
ASContext.Context.BuildCMD(false);
494-
e.Handled = true;
495-
}
482+
string cmdData = de.Data as string;
483+
// run MTASC
484+
if (command == "ASCompletion.CustomBuild")
485+
{
486+
if (cmdData != null) ASContext.Context.RunCMD(cmdData);
487+
else ASContext.Context.RunCMD("");
488+
e.Handled = true;
489+
}
496490

497-
// resolve element under cusor and open declaration
498-
else if (command == "ASCompletion.GotoDeclaration")
499-
{
500-
ASComplete.DeclarationLookup(sci);
501-
e.Handled = true;
502-
}
491+
// build the SWF using MTASC
492+
else if (command == "ASCompletion.QuickBuild")
493+
{
494+
ASContext.Context.BuildCMD(false);
495+
e.Handled = true;
496+
}
503497

504-
// resolve element under cursor and send a CustomData event
505-
else if (command == "ASCompletion.ResolveElement")
506-
{
507-
ASComplete.ResolveElement(sci, cmdData);
508-
e.Handled = true;
509-
}
510-
else if (command == "ASCompletion.MakeIntrinsic")
511-
{
512-
ASContext.Context.MakeIntrinsic(cmdData);
513-
e.Handled = true;
514-
}
498+
// resolve element under cursor and open declaration
499+
else if (command == "ASCompletion.GotoDeclaration")
500+
{
501+
ASComplete.DeclarationLookup(sci);
502+
e.Handled = true;
503+
}
515504

516-
// alternative to default shortcuts
517-
else if (command == "ASCompletion.CtrlSpace")
518-
{
519-
ASComplete.OnShortcut(Keys.Control | Keys.Space, ASContext.CurSciControl);
520-
e.Handled = true;
521-
}
522-
else if (command == "ASCompletion.CtrlShiftSpace")
523-
{
524-
ASComplete.OnShortcut(Keys.Control | Keys.Shift | Keys.Space, ASContext.CurSciControl);
525-
e.Handled = true;
526-
}
527-
else if (command == "ASCompletion.CtrlAltSpace")
528-
{
529-
ASComplete.OnShortcut(Keys.Control | Keys.Alt | Keys.Space, ASContext.CurSciControl);
530-
e.Handled = true;
531-
}
532-
else if (command == "ASCompletion.ContextualGenerator")
533-
{
534-
if (ASContext.HasContext && ASContext.Context.IsFileValid)
505+
// resolve element under cursor and send a CustomData event
506+
else if (command == "ASCompletion.ResolveElement")
535507
{
536-
ASGenerator.ContextualGenerator(ASContext.CurSciControl);
508+
ASComplete.ResolveElement(sci, cmdData);
509+
e.Handled = true;
510+
}
511+
else if (command == "ASCompletion.MakeIntrinsic")
512+
{
513+
ASContext.Context.MakeIntrinsic(cmdData);
514+
e.Handled = true;
515+
}
516+
517+
// alternative to default shortcuts
518+
else if (command == "ASCompletion.CtrlSpace")
519+
{
520+
ASComplete.OnShortcut(Keys.Control | Keys.Space, ASContext.CurSciControl);
521+
e.Handled = true;
522+
}
523+
else if (command == "ASCompletion.CtrlShiftSpace")
524+
{
525+
ASComplete.OnShortcut(Keys.Control | Keys.Shift | Keys.Space, ASContext.CurSciControl);
526+
e.Handled = true;
527+
}
528+
else if (command == "ASCompletion.CtrlAltSpace")
529+
{
530+
ASComplete.OnShortcut(Keys.Control | Keys.Alt | Keys.Space, ASContext.CurSciControl);
531+
e.Handled = true;
532+
}
533+
else if (command == "ASCompletion.ContextualGenerator")
534+
{
535+
if (ASContext.HasContext && ASContext.Context.IsFileValid)
536+
{
537+
var options = ASGenerator.ContextualGenerator(ASContext.CurSciControl);
538+
var dataEvent = new DataEvent(EventType.Command, "ASCompletion.ContextualGenerator.AddOptions", options);
539+
EventManager.DispatchEvent(this, dataEvent);
540+
CompletionList.Show(options, false);
541+
}
537542
}
538543
}
539-
}
540-
return;
544+
return;
541545

542-
case EventType.ProcessEnd:
543-
string procResult = (e as TextEvent).Value;
544-
ASContext.Context.OnProcessEnd(procResult);
545-
break;
546-
}
546+
case EventType.ProcessEnd:
547+
string procResult = (e as TextEvent).Value;
548+
ASContext.Context.OnProcessEnd(procResult);
549+
break;
550+
}
547551
}
548552
catch(Exception ex)
549553
{

External/Plugins/CodeRefactor/CodeRefactor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<Compile Include="Provider\MovingHelper.cs" />
114114
<Compile Include="Provider\RefactoringHelper.cs" />
115115
<Compile Include="Provider\UserInterfaceManager.cs" />
116+
<Compile Include="RefactorItem.cs" />
116117
<Compile Include="Settings.cs" />
117118
</ItemGroup>
118119
<ItemGroup>

External/Plugins/CodeRefactor/Commands/RefactorCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace CodeRefactor.Commands
55
{
66
/// <summary>
7-
/// Basic underyling Refactoring command. Refactoring commands can derive from this.
7+
/// Basic underlying Refactoring command. Refactoring commands can derive from this.
88
/// </summary>
99
/// <typeparam name="RefactorResultType">The refactoring results return type</typeparam>
1010
public abstract class RefactorCommand<RefactorResultType>

External/Plugins/CodeRefactor/PluginMain.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
139139
break;
140140

141141
case EventType.Command:
142-
if (settingObject.DisableMoveRefactoring) return;
143142
DataEvent de = (DataEvent)e;
144143
string[] args;
145144
string oldPath;
146145
string newPath;
147146
switch (de.Action)
148147
{
149148
case ProjectFileActionsEvents.FileRename:
149+
if (settingObject.DisableMoveRefactoring) break;
150150
args = de.Data as string[];
151151
oldPath = args[0];
152152
newPath = args[1];
@@ -163,6 +163,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
163163
break;
164164

165165
case ProjectFileActionsEvents.FileMove:
166+
if (settingObject.DisableMoveRefactoring) break;
166167
args = de.Data as string[];
167168
oldPath = args[0];
168169
newPath = args[1];
@@ -172,6 +173,10 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
172173
e.Handled = true;
173174
}
174175
break;
176+
177+
case "ASCompletion.ContextualGenerator.AddOptions":
178+
OnAddRefactorOptions(de.Data as List<ICompletionListItem>);
179+
break;
175180
}
176181
break;
177182
}
@@ -655,7 +660,34 @@ public void SaveSettings()
655660
ObjectSerializer.Serialize(this.settingFilename, this.settingObject);
656661
}
657662

658-
#endregion
663+
void OnAddRefactorOptions(List<ICompletionListItem> list)
664+
{
665+
if (list == null)
666+
return;
667+
668+
RefactorItem.AddItemToList(refactorMainMenu.RenameMenuItem, list);
669+
RefactorItem.AddItemToList(refactorMainMenu.ExtractMethodMenuItem, list);
670+
RefactorItem.AddItemToList(refactorMainMenu.ExtractLocalVariableMenuItem, list);
671+
RefactorItem.AddItemToList(refactorMainMenu.DelegateMenuItem, list);
672+
673+
var features = ASContext.Context.Features;
674+
675+
if (!features.hasImports)
676+
return;
659677

678+
var sci = ASContext.CurSciControl;
679+
string line = sci.GetLine(sci.CurrentLine).TrimStart();
680+
681+
if (line.StartsWith(features.importKey, StringComparison.Ordinal)
682+
|| !string.IsNullOrEmpty(features.importKeyAlt) && line.StartsWith(features.importKeyAlt, StringComparison.Ordinal))
683+
{
684+
RefactorItem.AddItemToList(refactorMainMenu.OrganizeMenuItem, list);
685+
686+
if (features.hasImportsWildcard)
687+
RefactorItem.AddItemToList(refactorMainMenu.TruncateMenuItem, list);
688+
}
689+
}
690+
691+
#endregion
660692
}
661693
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.Drawing;
3+
using System.Text.RegularExpressions;
4+
using System.Windows.Forms;
5+
using PluginCore;
6+
using PluginCore.Localization;
7+
8+
namespace CodeRefactor
9+
{
10+
class RefactorItem : ICompletionListItem
11+
{
12+
Bitmap icon;
13+
string description;
14+
string label;
15+
ToolStripItem item;
16+
17+
public static void AddItemToList(ToolStripItem item, List<ICompletionListItem> list)
18+
{
19+
if (item.Enabled)
20+
list.Add(new RefactorItem(item));
21+
}
22+
23+
public RefactorItem(ToolStripItem item)
24+
{
25+
this.item = item;
26+
label = Regex.Replace(item.Text, "[&.]", string.Empty);
27+
description = TextHelper.GetString("Label.Refactor").Replace("&", string.Empty);
28+
icon = new Bitmap(item.Image ?? PluginBase.MainForm.FindImage("452")); //452 or 473
29+
}
30+
31+
public string Description
32+
{
33+
get { return description; }
34+
}
35+
36+
public Bitmap Icon
37+
{
38+
get { return icon; }
39+
}
40+
41+
public string Label
42+
{
43+
get { return label; }
44+
}
45+
46+
public string Value
47+
{
48+
get
49+
{
50+
item.PerformClick();
51+
return null;
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)