Skip to content

Commit fbf4e5b

Browse files
committed
Fixes typing a wildcard import in the imports section and in function bodies
1 parent f1bcfe7 commit fbf4e5b

File tree

1 file changed

+54
-55
lines changed

1 file changed

+54
-55
lines changed

External/Plugins/ASCompletion/Completion/ASComplete.cs

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,74 +4274,73 @@ public static bool HasSnippet(string word)
42744274
/// <param name="sci"></param>
42754275
/// <param name="value">Character inserted</param>
42764276
/// <returns>Code was generated</returns>
4277-
static private bool CodeAutoOnChar(ScintillaControl sci, int value)
4277+
static bool CodeAutoOnChar(ScintillaControl sci, int value)
42784278
{
42794279
if (ASContext.Context.Settings == null || !ASContext.Context.Settings.GenerateImports)
42804280
return false;
42814281

42824282
int position = sci.CurrentPos;
42834283

4284-
if (value == '*' && position > 1 && sci.CharAt(position-2) == '.' && LastExpression != null)
4285-
{
4286-
// context
4287-
if (LastExpression.Separator == ' ' && LastExpression.WordBefore != null
4288-
&& !ASContext.Context.Features.HasTypePreKey(LastExpression.WordBefore))
4289-
return false;
4284+
if (value == '*' && position > 1 && sci.CharAt(position - 2) == '.' && LastExpression != null)
4285+
return HandleWildcardList(sci, position, LastExpression);
42904286

4291-
FileModel cFile = ASContext.Context.CurrentModel;
4292-
ClassModel cClass = ASContext.Context.CurrentClass;
4293-
ASResult context = EvalExpression(LastExpression.Value, LastExpression, cFile, cClass, true, false);
4294-
if (context.IsNull() || !context.IsPackage || context.InFile == null)
4295-
return false;
4287+
return false;
4288+
}
42964289

4297-
string package = LastExpression.Value;
4298-
int startPos = LastExpression.Position;
4299-
string check = "";
4300-
char c;
4301-
while (startPos > LastExpression.PositionExpression && check.Length <= package.Length && check != package)
4302-
{
4303-
c = (char)sci.CharAt(--startPos);
4304-
if (c > 32) check = c+check;
4305-
}
4306-
if (check != package)
4307-
return false;
4290+
/// <summary>
4291+
/// User entered a qualified package with a wildcard, eg. flash.geom.*, at an unexpected position, eg. not after 'import'.
4292+
/// Remove expression, generate coresponding wildcard import, and show list of types of this package
4293+
/// </summary>
4294+
static bool HandleWildcardList(ScintillaControl sci, int position, ASExpr expr)
4295+
{
4296+
// validate context
4297+
var context = ASContext.Context;
4298+
if (expr.Separator == ' ' && expr.WordBefore != null
4299+
&& context.Features.HasTypePreKey(expr.WordBefore))
4300+
return false;
43084301

4309-
// insert import
4310-
string statement = "import " + package + "*;" + LineEndDetector.GetNewLineMarker(sci.EOLMode);
4311-
int endPos = sci.CurrentPos;
4312-
int line = 0;
4313-
int curLine = sci.LineFromPosition(position);
4314-
bool found = false;
4315-
while (line < curLine)
4302+
var cFile = context.CurrentModel;
4303+
var cClass = context.CurrentClass;
4304+
var resolved = EvalExpression(expr.Value, expr, cFile, cClass, true, false);
4305+
if (resolved.IsNull() || !resolved.IsPackage || resolved.InFile == null)
4306+
return false;
4307+
4308+
string package = resolved.InFile.Package;
4309+
string check = Regex.Replace(expr.Value, "\\s", "").TrimEnd('.');
4310+
if (check != package)
4311+
return false;
4312+
4313+
sci.BeginUndoAction();
4314+
try
4315+
{
4316+
// remove temp wildcard
4317+
int startPos = expr.PositionExpression;
4318+
sci.SetSel(startPos, position);
4319+
sci.ReplaceSel("");
4320+
4321+
// generate import
4322+
if (context.Settings.GenerateImports)
43164323
{
4317-
if (sci.GetLine(line++).IndexOf("import") >= 0) found = true;
4318-
else if (found) {
4319-
line--;
4320-
break;
4324+
var wildcard = new MemberModel { Name = "*", Type = package + ".*" };
4325+
if (!context.IsImported(wildcard, sci.LineFromPosition(position)))
4326+
{
4327+
startPos += ASGenerator.InsertImport(wildcard, true);
4328+
sci.SetSel(startPos, startPos);
43214329
}
43224330
}
4323-
if (line == curLine) line = 0;
4324-
position = sci.PositionFromLine(line);
4325-
line = sci.FirstVisibleLine;
4326-
sci.SetSel(position, position);
4327-
sci.ReplaceSel(statement);
4328-
4329-
// prepare insertion of the term as usual
4330-
startPos += statement.Length;
4331-
endPos += statement.Length;
4332-
sci.SetSel(startPos, endPos);
4333-
sci.ReplaceSel("");
4334-
sci.LineScroll(0, line-sci.FirstVisibleLine+1);
4335-
4336-
// create classes list
4337-
List<ICompletionListItem> list = new List<ICompletionListItem>();
4338-
foreach(MemberModel import in cClass.InFile.Imports)
4339-
if (import.Type.StartsWith(package))
4340-
list.Add(new MemberItem(import));
4341-
CompletionList.Show(list, false);
4342-
return true;
43434331
}
4344-
return false;
4332+
finally
4333+
{
4334+
sci.EndUndoAction();
4335+
}
4336+
4337+
// show types
4338+
var list = new List<ICompletionListItem>();
4339+
var imports = context.ResolvePackage(package, false).Imports;
4340+
foreach (MemberModel import in imports)
4341+
list.Add(new MemberItem(import));
4342+
CompletionList.Show(list, false);
4343+
return true;
43454344
}
43464345

43474346
#endregion

0 commit comments

Comments
 (0)