Lua Text - start a very rough auto-complete box

This commit is contained in:
adelikat 2012-07-23 02:24:48 +00:00
parent 6379aafbae
commit 8b06a39ef9
3 changed files with 127 additions and 2 deletions

View File

@ -37,6 +37,20 @@ namespace BizHawk.MultiClient.tools
return libs.ToList();
}
public List<string> GetFunctionsByLibrary(string library)
{
List<string> functions = new List<string>();
for (int i = 0; i < FunctionList.Count; i++)
{
if (FunctionList[i].library == library)
{
functions.Add(FunctionList[i].name);
}
}
return functions;
}
public class LibraryFunction
{
public LibraryFunction(string method_lib, string method_name, System.Reflection.MethodInfo method)

View File

@ -41,6 +41,8 @@
this.fontToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.syntaxHighlightingToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.MessageLabel = new System.Windows.Forms.Label();
this.AutoCompleteView = new System.Windows.Forms.ListView();
this.PositionLabel = new System.Windows.Forms.Label();
this.LuaText = new BizHawk.MultiClient.LuaWriterBox();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
@ -135,6 +137,27 @@
this.MessageLabel.TabIndex = 2;
this.MessageLabel.Text = " ";
//
// AutoCompleteView
//
this.AutoCompleteView.FullRowSelect = true;
this.AutoCompleteView.Location = new System.Drawing.Point(695, 322);
this.AutoCompleteView.Name = "AutoCompleteView";
this.AutoCompleteView.Size = new System.Drawing.Size(121, 97);
this.AutoCompleteView.TabIndex = 3;
this.AutoCompleteView.UseCompatibleStateImageBehavior = false;
this.AutoCompleteView.View = System.Windows.Forms.View.List;
this.AutoCompleteView.Visible = false;
this.AutoCompleteView.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.AutoCompleteView_MouseDoubleClick);
//
// PositionLabel
//
this.PositionLabel.AutoSize = true;
this.PositionLabel.Location = new System.Drawing.Point(14, 30);
this.PositionLabel.Name = "PositionLabel";
this.PositionLabel.Size = new System.Drawing.Size(46, 13);
this.PositionLabel.TabIndex = 4;
this.PositionLabel.Text = " ";
//
// LuaText
//
this.LuaText.AcceptsTab = true;
@ -143,14 +166,15 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.LuaText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.LuaText.EnableAutoDragDrop = true;
this.LuaText.Location = new System.Drawing.Point(12, 29);
this.LuaText.Location = new System.Drawing.Point(15, 50);
this.LuaText.Name = "LuaText";
this.LuaText.Size = new System.Drawing.Size(819, 392);
this.LuaText.Size = new System.Drawing.Size(813, 369);
this.LuaText.TabIndex = 0;
this.LuaText.Text = "";
this.LuaText.WordWrap = false;
this.LuaText.ZoomFactor = 2F;
this.LuaText.TextChanged += new System.EventHandler(this.LuaText_TextChanged);
this.LuaText.KeyDown += new System.Windows.Forms.KeyEventHandler(this.LuaText_KeyDown);
this.LuaText.KeyUp += new System.Windows.Forms.KeyEventHandler(this.LuaText_KeyUp);
//
// LuaWriter
@ -159,6 +183,8 @@
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(843, 441);
this.Controls.Add(this.PositionLabel);
this.Controls.Add(this.AutoCompleteView);
this.Controls.Add(this.MessageLabel);
this.Controls.Add(this.LuaText);
this.Controls.Add(this.menuStrip1);
@ -190,5 +216,7 @@
private System.Windows.Forms.ToolStripMenuItem configToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fontToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem syntaxHighlightingToolStripMenuItem;
private System.Windows.Forms.ListView AutoCompleteView;
private System.Windows.Forms.Label PositionLabel;
}
}

View File

@ -15,6 +15,7 @@ namespace BizHawk.MultiClient
public partial class LuaWriter : Form
{
//TODO:
//fix tabs (tab should be 4 characters)
//Line numbers
//Option to toggle line numbers
//Go to line number Ctrl+G
@ -409,6 +410,12 @@ namespace BizHawk.MultiClient
private void LuaText_KeyUp(object sender, KeyEventArgs e)
{
hasChanged = true;
int currentLineIndex = LuaText.GetLineFromCharIndex(LuaText.SelectionStart);
int lastLineIndex = LuaText.GetLineFromCharIndex(LuaText.TextLength);
int currentColumnIndex = LuaText.SelectionStart - LuaText.GetFirstCharIndexFromLine(currentLineIndex);
PositionLabel.Text = string.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1);
}
private void Changes()
@ -443,5 +450,81 @@ namespace BizHawk.MultiClient
}
}
private void LuaText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
AutoCompleteView.Visible = false;
}
if (e.KeyCode == Keys.OemPeriod)
{
string currentword = CurrentWord();
if (IsLibraryWord(currentword))
{
List<string> libfunctions = Global.MainForm.LuaConsole1.LuaImp.docs.GetFunctionsByLibrary(currentword);
AutoCompleteView.Visible = true;
AutoCompleteView.Items.Clear();
foreach(string function in libfunctions)
{
ListViewItem item = new ListViewItem(function);
AutoCompleteView.Items.Add(item);
}
AutoCompleteView.Location = new Point(0, 0);
}
}
}
private string CurrentWord()
{
int last = LuaText.SelectionStart;
int lastSpace = LuaText.Text.Substring(0, last).LastIndexOf(' ');
int lastLine = LuaText.Text.Substring(0, last).LastIndexOf('\n');
int start = 0;
if (lastSpace > lastLine)
{
start = lastSpace;
}
else
{
start = lastLine;
}
if (start == -1)
{
start = 0;
}
int length = last - start - 1;
string word = LuaText.Text.Substring(start + 1, length);
return word;
}
private bool IsLibraryWord(string word)
{
List<string> Libs = Global.MainForm.LuaConsole1.LuaImp.docs.GetLibraryList();
if (Libs.Contains(word))
{
return true;
}
return false;
}
private void AutoCompleteView_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListView.SelectedIndexCollection indexes = AutoCompleteView.SelectedIndices;
if (indexes.Count > 0)
{
string str = AutoCompleteView.Items[indexes[0]].Text;
int start = LuaText.SelectionStart;
LuaText.Text = LuaText.Text.Insert(start, str);
AutoCompleteView.Visible = false;
}
}
}
}