private static void AssignStatementToVar(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member) { int lineNum = Sci.LineFromPosition(Sci.CurrentPos); string line = Sci.GetLine(lineNum); StatementReturnType returnType = GetStatementReturnType(Sci, inClass, line, Sci.PositionFromLine(lineNum)); if (returnType == null) { return; } IASContext cntx = inClass.InFile.Context; bool isAs3 = cntx.Settings.LanguageId == "AS3"; string voidWord = isAs3 ? "void" : "Void"; string type = null; string varname = null; string cleanType = null; ASResult resolve = returnType.resolve; int pos = returnType.position; string word = returnType.word; if (resolve != null && !resolve.IsNull()) { if (resolve.Member != null && resolve.Member.Type != null) { type = resolve.Member.Type; } else if (resolve.Type != null && resolve.Type.Name != null) { type = returnType.resolve.Type.QualifiedName; } if (resolve.Member != null && resolve.Member.Name != null) { varname = GuessVarName(resolve.Member.Name, type); } } if (word != null && Char.IsDigit(word[0])) word = null; if (type == voidWord) type = null; if (varname == null) varname = GuessVarName(word, type); if (varname != null && varname == word) varname = varname.Length == 1 ? varname + "1" : varname[0] + ""; if (type != null) cleanType = FormatType(GetShortType(type)); string template = TemplateUtils.GetTemplate("AssignVariable"); if (varname != null) template = TemplateUtils.ReplaceTemplateVariable(template, "Name", varname); else template = TemplateUtils.ReplaceTemplateVariable(template, "Name", null); if (cleanType != null) template = TemplateUtils.ReplaceTemplateVariable(template, "Type", cleanType); else template = TemplateUtils.ReplaceTemplateVariable(template, "Type", null); int indent = Sci.GetLineIndentation(lineNum); pos = Sci.PositionFromLine(lineNum) + indent / Sci.Indent; Sci.CurrentPos = pos; Sci.SetSel(pos, pos); InsertCode(pos, template); if (type != null) { ClassModel inClassForImport = null; if (resolve.InClass != null) { inClassForImport = resolve.InClass; } else if (resolve.RelClass != null) { inClassForImport = resolve.RelClass; } else { inClassForImport = inClass; } List<string> l = new List<string>(); l.Add(getQualifiedType(type, inClassForImport)); pos += AddImportsByName(l, Sci.LineFromPosition(pos)); } }
static public void GenerateOverride(ScintillaNet.ScintillaControl Sci, ClassModel ofClass, MemberModel member, int position) { ContextFeatures features = ASContext.Context.Features; List<string> typesUsed = new List<string>(); bool isProxy = (member.Namespace == "flash_proxy"); if (isProxy) typesUsed.Add("flash.utils.flash_proxy"); bool isAS2Event = ASContext.Context.Settings.LanguageId == "AS2" && member.Name.StartsWith("on"); bool isObjectMethod = ofClass.QualifiedName == "Object"; int line = Sci.LineFromPosition(position); string currentText = Sci.GetLine(line); int startPos = currentText.Length; GetStartPos(currentText, ref startPos, features.privateKey); GetStartPos(currentText, ref startPos, features.protectedKey); GetStartPos(currentText, ref startPos, features.internalKey); GetStartPos(currentText, ref startPos, features.publicKey); GetStartPos(currentText, ref startPos, features.staticKey); GetStartPos(currentText, ref startPos, features.overrideKey); startPos += Sci.PositionFromLine(line); FlagType flags = member.Flags; string acc = ""; string decl = ""; if (features.hasNamespaces && !string.IsNullOrEmpty(member.Namespace) && member.Namespace != "internal") acc = member.Namespace; else if ((member.Access & Visibility.Public) > 0) acc = features.publicKey; else if ((member.Access & Visibility.Internal) > 0) acc = features.internalKey; else if ((member.Access & Visibility.Protected) > 0) acc = features.protectedKey; else if ((member.Access & Visibility.Private) > 0 && features.methodModifierDefault != Visibility.Private) acc = features.privateKey; bool isStatic = (flags & FlagType.Static) > 0; if (isStatic) acc = features.staticKey + " " + acc; if (!isAS2Event && !isObjectMethod) acc = features.overrideKey + " " + acc; acc = Regex.Replace(acc, "[ ]+", " ").Trim(); if ((flags & (FlagType.Getter | FlagType.Setter)) > 0) { string type = member.Type; string name = member.Name; if (member.Parameters != null && member.Parameters.Count == 1) type = member.Parameters[0].Type; type = FormatType(type); if (type == null && !features.hasInference) type = features.objectKey; bool genGetter = ofClass.Members.Search(name, FlagType.Getter, 0) != null; bool genSetter = ofClass.Members.Search(name, FlagType.Setter, 0) != null; if (isHaxe) { // property is public but not the methods acc = features.overrideKey; } if (genGetter) { string tpl = TemplateUtils.GetTemplate("OverrideGetter", "Getter"); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Modifiers", acc); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Name", name); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Type", type); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Member", "super." + name); decl += tpl; } if (genSetter) { string tpl = TemplateUtils.GetTemplate("OverrideSetter", "Setter"); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Modifiers", acc); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Name", name); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Type", type); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Member", "super." + name); tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Void", ASContext.Context.Features.voidKey ?? "void"); if (decl.Length > 0) { tpl = "\n\n" + tpl.Replace("$(EntryPoint)", ""); } decl += tpl; } decl = TemplateUtils.ReplaceTemplateVariable(decl, "BlankLine", ""); } else { string type = FormatType(member.Type); //if (type == null) type = features.objectKey; decl = acc + features.functionKey + " "; bool noRet = type == null || type.Equals("void", StringComparison.OrdinalIgnoreCase); type = (noRet && type != null) ? ASContext.Context.Features.voidKey : type; if (!noRet) { string qType = getQualifiedType(type, ofClass); typesUsed.Add(qType); if (qType == type) { ClassModel rType = ASContext.Context.ResolveType(type, ofClass.InFile); if (!rType.IsVoid()) type = rType.Name; } } string action = (isProxy || isAS2Event) ? "" : GetSuperCall(member, typesUsed, ofClass); string template = TemplateUtils.GetTemplate("MethodOverride"); // fix parameters if needed if (member.Parameters != null) foreach (MemberModel para in member.Parameters) if (para.Type == "any") para.Type = "*"; template = TemplateUtils.ReplaceTemplateVariable(template, "Modifiers", acc); template = TemplateUtils.ReplaceTemplateVariable(template, "Name", member.Name); template = TemplateUtils.ReplaceTemplateVariable(template, "Arguments", TemplateUtils.ParametersString(member, true)); template = TemplateUtils.ReplaceTemplateVariable(template, "Type", type); template = TemplateUtils.ReplaceTemplateVariable(template, "Method", action); decl = template; } Sci.BeginUndoAction(); try { if (ASContext.Context.Settings.GenerateImports && typesUsed.Count > 0) { int offset = AddImportsByName(typesUsed, line); position += offset; startPos += offset; } Sci.SetSel(startPos, position + member.Name.Length); InsertCode(startPos, decl); } finally { Sci.EndUndoAction(); } }
static public void ContextualGenerator(ScintillaNet.ScintillaControl Sci) { if (ASContext.Context is ASContext) (ASContext.Context as ASContext).UpdateCurrentFile(false); // update model if ((ASContext.Context.CurrentClass.Flags & (FlagType.Enum | FlagType.TypeDef)) > 0) return; lookupPosition = -1; int position = Sci.CurrentPos; if (Sci.BaseStyleAt(position) == 19) // on keyword return; bool isNotInterface = (ASContext.Context.CurrentClass.Flags & FlagType.Interface) == 0; int line = Sci.LineFromPosition(position); contextToken = Sci.GetWordFromPosition(position); contextMatch = null; FoundDeclaration found = GetDeclarationAtLine(Sci, line); string text = Sci.GetLine(line); bool suggestItemDeclaration = false; if (isNotInterface && !String.IsNullOrEmpty(contextToken) && Char.IsDigit(contextToken[0])) { ShowConvertToConst(found); return; } ASResult resolve = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(position, true)); contextResolved = resolve; // ignore automatic vars (MovieClip members) if (isNotInterface && resolve.Member != null && (((resolve.Member.Flags & FlagType.AutomaticVar) > 0) || (resolve.InClass != null && resolve.InClass.QualifiedName == "Object"))) { resolve.Member = null; resolve.Type = null; } if (isNotInterface && found.inClass != ClassModel.VoidClass && contextToken != null) { if (resolve.Member == null && resolve.Type != null && (resolve.Type.Flags & FlagType.Interface) > 0) // implement interface { contextParam = resolve.Type.Type; ShowImplementInterface(found); return; } if (resolve.Member != null && !ASContext.Context.CurrentClass.IsVoid() && (resolve.Member.Flags & FlagType.LocalVar) > 0) // promote to class var { contextMember = resolve.Member; ShowPromoteLocalAndAddParameter(found); return; } } if (contextToken != null && resolve.Member == null) // import declaration { if ((resolve.Type == null || resolve.Type.IsVoid() || !ASContext.Context.IsImported(resolve.Type, line)) && CheckAutoImport(found)) return; if (resolve.Type == null) { int stylemask = (1 << Sci.StyleBits) - 1; suggestItemDeclaration = ASComplete.IsTextStyle(Sci.StyleAt(position - 1) & stylemask); } } if (isNotInterface && found.member != null) { // private var -> property if ((found.member.Flags & FlagType.Variable) > 0 && (found.member.Flags & FlagType.LocalVar) == 0) { // maybe we just want to import the member's non-imported type Match m = Regex.Match(text, String.Format(patternVarDecl, found.member.Name, contextToken)); if (m.Success) { contextMatch = m; ClassModel type = ASContext.Context.ResolveType(contextToken, ASContext.Context.CurrentModel); if (type.IsVoid() && CheckAutoImport(found)) return; } ShowGetSetList(found); return; } // inside a function else if ((found.member.Flags & (FlagType.Function | FlagType.Getter | FlagType.Setter)) > 0 && resolve.Member == null && resolve.Type == null) { if (contextToken != null) { // "generate event handlers" suggestion string re = String.Format(patternEvent, contextToken); Match m = Regex.Match(text, re, RegexOptions.IgnoreCase); if (m.Success) { contextMatch = m; contextParam = CheckEventType(m.Groups["event"].Value); ShowEventList(found); return; } m = Regex.Match(text, String.Format(patternAS2Delegate, contextToken), RegexOptions.IgnoreCase); if (m.Success) { contextMatch = m; ShowDelegateList(found); return; } // suggest delegate if (ASContext.Context.Features.hasDelegates) { m = Regex.Match(text, @"([a-z0-9_.]+)\s*\+=\s*" + contextToken, RegexOptions.IgnoreCase); if (m.Success) { int offset = Sci.PositionFromLine(Sci.LineFromPosition(position)) + m.Groups[1].Index + m.Groups[1].Length; resolve = ASComplete.GetExpressionType(Sci, offset); if (resolve.Member != null) contextMember = ResolveDelegate(resolve.Member.Type, resolve.InFile); contextMatch = m; ShowDelegateList(found); return; } } } else { // insert a default handler name, then "generate event handlers" suggestion Match m = Regex.Match(text, String.Format(patternEvent, ""), RegexOptions.IgnoreCase); if (m.Success) { int regexIndex = m.Index + Sci.PositionFromLine(Sci.CurrentLine); GenerateDefaultHandlerName(Sci, position, regexIndex, m.Groups["event"].Value, true); resolve = ASComplete.GetExpressionType(Sci, Sci.CurrentPos); if (resolve.Member == null || (resolve.Member.Flags & FlagType.AutomaticVar) > 0) { contextMatch = m; contextParam = CheckEventType(m.Groups["event"].Value); ShowEventList(found); } return; } // insert default delegate name, then "generate delegate" suggestion if (ASContext.Context.Features.hasDelegates) { m = Regex.Match(text, @"([a-z0-9_.]+)\s*\+=\s*", RegexOptions.IgnoreCase); if (m.Success) { int offset = Sci.PositionFromLine(Sci.LineFromPosition(position)) + m.Groups[1].Index + m.Groups[1].Length; resolve = ASComplete.GetExpressionType(Sci, offset); if (resolve.Member != null) { contextMember = ResolveDelegate(resolve.Member.Type, resolve.InFile); string delegateName = resolve.Member.Name; if (delegateName.StartsWith("on")) delegateName = delegateName.Substring(2); GenerateDefaultHandlerName(Sci, position, offset, delegateName, false); resolve = ASComplete.GetExpressionType(Sci, Sci.CurrentPos); if (resolve.Member == null || (resolve.Member.Flags & FlagType.AutomaticVar) > 0) { contextMatch = m; ShowDelegateList(found); } return; } } } } } // "Generate fields from parameters" suggestion if (found.member != null && (found.member.Flags & FlagType.Function) > 0 && found.member.Parameters != null && (found.member.Parameters.Count > 0) && resolve.Member != null && (resolve.Member.Flags & FlagType.ParameterVar) > 0) { contextMember = resolve.Member; ShowFieldFromParameter(found); return; } // "add to interface" suggestion if (resolve.Member != null && resolve.Member.Name == found.member.Name && line == found.member.LineFrom && ((found.member.Flags & FlagType.Function) > 0 || (found.member.Flags & FlagType.Getter) > 0 || (found.member.Flags & FlagType.Setter) > 0) && found.inClass != ClassModel.VoidClass && found.inClass.Implements != null && found.inClass.Implements.Count > 0) { string funcName = found.member.Name; FlagType flags = found.member.Flags & ~FlagType.Access; List<string> interfaces = new List<string>(); foreach (string interf in found.inClass.Implements) { bool skip = false; ClassModel cm = ASContext.Context.ResolveType(interf, ASContext.Context.CurrentModel); foreach (MemberModel m in cm.Members) { if (m.Name.Equals(funcName) && m.Flags.Equals(flags)) { skip = true; break; } } if (!skip) { interfaces.Add(interf); } } if (interfaces.Count > 0) { ShowAddInterfaceDefList(found, interfaces); return; } } // "assign var to statement" suggestion int curLine = Sci.CurrentLine; string ln = Sci.GetLine(curLine).TrimEnd(); if (ln.Length > 0 && ln.IndexOf("=") == -1 && ln.Length <= Sci.CurrentPos - Sci.PositionFromLine(curLine)) // cursor at end of line { ShowAssignStatementToVarList(found); return; } } // suggest generate constructor / toString if (isNotInterface && found.member == null && found.inClass != ClassModel.VoidClass && contextToken == null) { bool hasConstructor = false; bool hasToString = false; foreach (MemberModel m in ASContext.Context.CurrentClass.Members) { if (!hasConstructor && (m.Flags & FlagType.Constructor) > 0) hasConstructor = true; if (!hasToString && (m.Flags & FlagType.Function) > 0 && m.Name.Equals("toString")) hasToString = true; } if (!hasConstructor || !hasToString) { ShowConstructorAndToStringList(found, hasConstructor, hasToString); return; } } if (isNotInterface && resolve.Member != null && resolve.Type != null && resolve.Type.QualifiedName == "String" && found.inClass != ClassModel.VoidClass) { int lineStartPos = Sci.PositionFromLine(Sci.CurrentLine); string lineStart = text.Substring(0, Sci.CurrentPos - lineStartPos); Match m = Regex.Match(lineStart, String.Format(@"new\s+(?<event>\w+)\s*\(\s*\w+", lineStart)); if (m.Success) { Group g = m.Groups["event"]; ASResult eventResolve = ASComplete.GetExpressionType(Sci, lineStartPos + g.Index + g.Length); if (eventResolve != null && eventResolve.Type != null) { ClassModel aType = eventResolve.Type; aType.ResolveExtends(); while (!aType.IsVoid() && aType.QualifiedName != "Object") { if (aType.QualifiedName == "flash.events.Event") { contextParam = eventResolve.Type.QualifiedName; ShowEventMetatagList(found); return; } aType = aType.Extends; } } } } // suggest declaration if (contextToken != null) { if (suggestItemDeclaration) { Match m = Regex.Match(text, String.Format(patternClass, contextToken)); if (m.Success) { contextMatch = m; ShowNewClassList(found); } else if (!found.inClass.IsVoid()) { m = Regex.Match(text, String.Format(patternMethod, contextToken)); if (m.Success) { contextMatch = m; ShowNewMethodList(found); } else ShowNewVarList(found); } } else { if (resolve != null && resolve.InClass != null && resolve.InClass.InFile != null && resolve.Member != null && (resolve.Member.Flags & FlagType.Function) > 0 && File.Exists(resolve.InClass.InFile.FileName) && !resolve.InClass.InFile.FileName.StartsWith(PathHelper.AppDir)) { Match m = Regex.Match(text, String.Format(patternMethodDecl, contextToken)); Match m2 = Regex.Match(text, String.Format(patternMethod, contextToken)); if (!m.Success && m2.Success) { contextMatch = m; ShowChangeMethodDeclList(found); } } else if (resolve != null && resolve.Type != null && resolve.Type.InFile != null && resolve.RelClass != null && File.Exists(resolve.Type.InFile.FileName) && !resolve.Type.InFile.FileName.StartsWith(PathHelper.AppDir)) { Match m = Regex.Match(text, String.Format(patternClass, contextToken)); if (m.Success) { contextMatch = m; ShowChangeConstructorDeclList(found); } } } } // TODO: Empty line, show generators list? }
public static int GetBodyStart(int lineFrom, int lineTo, ScintillaNet.ScintillaControl Sci, int pos) { int posStart = Sci.PositionFromLine(lineFrom); int posEnd = Sci.LineEndPosition(lineTo); Sci.SetSel(posStart, posEnd); List<char> characterClass = new List<char>(new char[] { ' ', '\r', '\n', '\t' }); string currentMethodBody = Sci.SelText; int nCount = 0; int funcBodyStart = pos; int extraLine = 0; if (pos == -1) { funcBodyStart = posStart + currentMethodBody.IndexOf('{'); extraLine = 1; } while (funcBodyStart <= posEnd) { char c = (char)Sci.CharAt(++funcBodyStart); if (c == '}') { int ln = Sci.LineFromPosition(funcBodyStart); int indent = Sci.GetLineIndentation(ln); if (lineFrom == lineTo || lineFrom == ln) { Sci.InsertText(funcBodyStart, Sci.NewLineMarker); Sci.SetLineIndentation(ln + 1, indent); ln++; } Sci.SetLineIndentation(ln, indent + Sci.Indent); Sci.InsertText(funcBodyStart, Sci.NewLineMarker); Sci.SetLineIndentation(ln + 1, indent); Sci.SetLineIndentation(ln, indent + Sci.Indent); funcBodyStart = Sci.LineEndPosition(ln); break; } else if (!characterClass.Contains(c)) { break; } else if (Sci.EOLMode == 1 && c == '\r' && (++nCount) > extraLine) { break; } else if (c == '\n' && (++nCount) > extraLine) { if (Sci.EOLMode != 2) { funcBodyStart--; } break; } } return funcBodyStart; }
private static void GenerateFunctionJob(GeneratorJobType job, ScintillaNet.ScintillaControl Sci, MemberModel member, bool detach, ClassModel inClass) { int position = 0; MemberModel latest = null; bool isOtherClass = false; Visibility funcVisi = job.Equals(GeneratorJobType.FunctionPublic) ? Visibility.Public : GetDefaultVisibility(); int wordPos = Sci.WordEndPosition(Sci.CurrentPos, true); List<FunctionParameter> functionParameters = ParseFunctionParameters(Sci, wordPos); // evaluate, if the function should be generated in other class ASResult funcResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true)); int contextOwnerPos = GetContextOwnerEndPos(Sci, Sci.WordStartPosition(Sci.CurrentPos, true)); MemberModel isStatic = new MemberModel(); if (contextOwnerPos != -1) { ASResult contextOwnerResult = ASComplete.GetExpressionType(Sci, contextOwnerPos); if (contextOwnerResult != null) { if (contextOwnerResult.Member == null && contextOwnerResult.Type != null) { isStatic.Flags |= FlagType.Static; } } } else if (member != null && (member.Flags & FlagType.Static) > 0) { isStatic.Flags |= FlagType.Static; } if (funcResult.RelClass != null && !funcResult.RelClass.IsVoid() && !funcResult.RelClass.Equals(inClass)) { AddLookupPosition(); lookupPosition = -1; DockContent dc = ASContext.MainForm.OpenEditableDocument(funcResult.RelClass.InFile.FileName, true); Sci = ASContext.CurSciControl; isOtherClass = true; FileModel fileModel = new FileModel(); fileModel.Context = ASContext.Context; ASFileParser parser = new ASFileParser(); parser.ParseSrc(fileModel, Sci.Text); foreach (ClassModel cm in fileModel.Classes) { if (cm.QualifiedName.Equals(funcResult.RelClass.QualifiedName)) { funcResult.RelClass = cm; break; } } inClass = funcResult.RelClass; ASContext.Context.UpdateContext(inClass.LineFrom); } string blockTmpl = null; if ((isStatic.Flags & FlagType.Static) > 0) { blockTmpl = TemplateUtils.GetBoundary("StaticMethods"); } else if ((funcVisi & Visibility.Public) > 0) { blockTmpl = TemplateUtils.GetBoundary("PublicMethods"); } else { blockTmpl = TemplateUtils.GetBoundary("PrivateMethods"); } latest = TemplateUtils.GetTemplateBlockMember(Sci, blockTmpl); if (latest == null || (!isOtherClass && member == null)) { latest = GetLatestMemberForFunction(inClass, funcVisi, isStatic); // if we generate function in current class.. if (!isOtherClass) { MethodsGenerationLocations location = ASContext.CommonSettings.MethodsGenerationLocations; if (member == null) { detach = false; lookupPosition = -1; position = Sci.WordStartPosition(Sci.CurrentPos, true); Sci.SetSel(position, Sci.WordEndPosition(position, true)); } else if (latest != null && location == MethodsGenerationLocations.AfterSimilarAccessorMethod) { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); Sci.SetSel(position, position); } else { position = Sci.PositionFromLine(member.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); Sci.SetSel(position, position); } } else // if we generate function in another class.. { if (latest != null) { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); } else { position = GetBodyStart(inClass.LineFrom, inClass.LineTo, Sci); detach = false; } Sci.SetSel(position, position); } } else { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); Sci.SetSel(position, position); } // add imports to function argument types if (functionParameters.Count > 0) { List<string> l = new List<string>(); foreach (FunctionParameter fp in functionParameters) { try { l.Add(fp.paramQualType); } catch (Exception) { } } int o = AddImportsByName(l, Sci.LineFromPosition(position)); position += o; if (latest == null) Sci.SetSel(position, Sci.WordEndPosition(position, true)); else Sci.SetSel(position, position); } List<MemberModel> parameters = new List<MemberModel>(); for (int i = 0; i < functionParameters.Count; i++) { string name = functionParameters[i].paramName; string type = functionParameters[i].paramType; parameters.Add(new MemberModel(name, type, FlagType.ParameterVar, 0)); } MemberModel newMember = NewMember(contextToken, isStatic, FlagType.Function, funcVisi); newMember.Parameters = parameters; GenerateFunction(newMember, position, detach, inClass); }
private static void AssignStatementToVar(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member) { int lineNum = Sci.CurrentLine; string line = Sci.GetLine(lineNum); StatementReturnType returnType = GetStatementReturnType(Sci, inClass, line, Sci.PositionFromLine(lineNum)); if (returnType == null) return; string type = null; string varname = null; ASResult resolve = returnType.resolve; string word = returnType.word; if (resolve != null && !resolve.IsNull()) { if (resolve.Member != null && resolve.Member.Type != null) { type = resolve.Member.Type; } else if (resolve.Type != null && resolve.Type.Name != null) { type = resolve.Type.QualifiedName; } if (resolve.Member != null && resolve.Member.Name != null) { varname = GuessVarName(resolve.Member.Name, type); } } if (!string.IsNullOrEmpty(word) && Char.IsDigit(word[0])) word = null; if (!string.IsNullOrEmpty(word) && (string.IsNullOrEmpty(type) || Regex.IsMatch(type, "(<[^]]+>)"))) word = null; if (!string.IsNullOrEmpty(type) && type.Equals("void", StringComparison.OrdinalIgnoreCase)) type = null; if (varname == null) varname = GuessVarName(word, type); if (varname != null && varname == word) varname = varname.Length == 1 ? varname + "1" : varname[0] + ""; string cleanType = null; if (type != null) cleanType = FormatType(GetShortType(type)); string template = TemplateUtils.GetTemplate("AssignVariable"); template = TemplateUtils.ReplaceTemplateVariable(template, "Name", varname); template = TemplateUtils.ReplaceTemplateVariable(template, "Type", cleanType); int indent = Sci.GetLineIndentation(lineNum); int pos = Sci.PositionFromLine(lineNum) + indent / Sci.Indent; Sci.CurrentPos = pos; Sci.SetSel(pos, pos); InsertCode(pos, template); if (type != null) { ClassModel inClassForImport = null; if (resolve.InClass != null) { inClassForImport = resolve.InClass; } else if (resolve.RelClass != null) { inClassForImport = resolve.RelClass; } else { inClassForImport = inClass; } List<string> l = new List<string>(); l.Add(getQualifiedType(type, inClassForImport)); pos += AddImportsByName(l, Sci.LineFromPosition(pos)); } }
private static void AddInterfaceDefJob(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member, string interf) { ClassModel aType = ASContext.Context.ResolveType(interf, ASContext.Context.CurrentModel); if (aType.IsVoid()) return; FileModel fileModel = ASFileParser.ParseFile(ASContext.Context.CreateFileModel(aType.InFile.FileName)); foreach (ClassModel cm in fileModel.Classes) { if (cm.QualifiedName.Equals(aType.QualifiedName)) { aType = cm; break; } } string template = TemplateUtils.GetTemplate("IFunction"); if ((member.Flags & FlagType.Getter) > 0) { template = TemplateUtils.GetTemplate("IGetter"); } else if ((member.Flags & FlagType.Setter) > 0) { template = TemplateUtils.GetTemplate("ISetter"); } DockContent dc = ASContext.MainForm.OpenEditableDocument(aType.InFile.FileName, true); Sci = ASContext.CurSciControl; MemberModel latest = GetLatestMemberForFunction(aType, Visibility.Default, new MemberModel()); int position; if (latest == null) { position = GetBodyStart(aType.LineFrom, aType.LineTo, Sci); } else { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); template = NewLine + template; } Sci.SetSel(position, position); Sci.CurrentPos = position; IASContext context = ASContext.Context; ContextFeatures features = context.Features; template = TemplateUtils.ToDeclarationString(member, template); template = TemplateUtils.ReplaceTemplateVariable(template, "BlankLine", NewLine); template = TemplateUtils.ReplaceTemplateVariable(template, "Void", features.voidKey); List<string> importsList = new List<string>(); string t; List<MemberModel> parms = member.Parameters; if (parms != null && parms.Count > 0) { for (int i = 0; i < parms.Count; i++) { if (parms[i].Type != null) { t = getQualifiedType(parms[i].Type, inClass); importsList.Add(t); } } } if (member.Type != null) { t = getQualifiedType(member.Type, inClass); importsList.Add(t); } if (importsList.Count > 0) { int o = AddImportsByName(importsList, Sci.LineFromPosition(position)); position += o; } Sci.SetSel(position, position); Sci.CurrentPos = position; InsertCode(position, template); }
private static void GenerateToString(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member) { bool isOverride = false; inClass.ResolveExtends(); if (inClass.Extends != null) { ClassModel aType = inClass.Extends; while (!aType.IsVoid() && aType.QualifiedName != "Object") { foreach (MemberModel method in aType.Members) { if (method.Name == "toString") { isOverride = true; break; } } if (isOverride) { break; } // interface inheritance aType = aType.Extends; } } MemberList members = inClass.Members; StringBuilder membersString = new StringBuilder(); StringBuilder oneMembersString; int len = 0; int indent = Sci.GetLineIndentation(Sci.LineFromPosition(Sci.CurrentPos)); foreach (MemberModel m in members) { if (((m.Flags & FlagType.Variable) > 0 || (m.Flags & FlagType.Getter) > 0) && (m.Access & Visibility.Public) > 0 && (m.Flags & FlagType.Constant) == 0) { oneMembersString = new StringBuilder(); oneMembersString.Append(" ").Append(m.Name).Append("=\" + ").Append(m.Name).Append(" + "); membersString.Append(oneMembersString); len += oneMembersString.Length; if (len > 80) { len = 0; membersString.Append("\n\t\t\t\t"); } membersString.Append("\""); } } member = new MemberModel("toString", "String", FlagType.Function, Visibility.Public); string repl = GetDeclaration(member, true).Replace("()", "($(EntryPoint))"); if (isOverride) { repl = "override " + repl; } string tmpl = GetTemplate("ToString", "{0} $(CSLB){{\n\treturn {1};\n}}"); string toStringData = "\"[" + inClass.Name + membersString.ToString() + "]\""; string decl = String.Format(tmpl, repl, toStringData); InsertCode(Sci.CurrentPos, decl); }
static public void GenerateOverride(ScintillaNet.ScintillaControl Sci, ClassModel ofClass, MemberModel member, int position) { ContextFeatures features = ASContext.Context.Features; List<string> typesUsed = new List<string>(); bool isProxy = (member.Namespace == "flash_proxy"); if (isProxy) typesUsed.Add("flash.utils.flash_proxy"); bool isAS2Event = ASContext.Context.Settings.LanguageId == "AS2" && member.Name.StartsWith("on"); bool isObjectMethod = ofClass.QualifiedName == "Object"; int line = Sci.LineFromPosition(position); string currentText = Sci.GetLine(line); int startPos = currentText.Length; GetStartPos(currentText, ref startPos, features.privateKey); GetStartPos(currentText, ref startPos, features.protectedKey); GetStartPos(currentText, ref startPos, features.internalKey); GetStartPos(currentText, ref startPos, features.publicKey); GetStartPos(currentText, ref startPos, features.staticKey); GetStartPos(currentText, ref startPos, features.overrideKey); startPos += Sci.PositionFromLine(line); FlagType flags = member.Flags; string acc = ""; string decl = ""; if (features.hasNamespaces && member.Namespace != null && member.Namespace.Length > 0 && member.Namespace != "internal") acc = member.Namespace; else if ((member.Access & Visibility.Public) > 0) acc = features.publicKey; else if ((member.Access & Visibility.Internal) > 0) acc = features.internalKey; else if ((member.Access & Visibility.Protected) > 0) acc = features.protectedKey; else if ((member.Access & Visibility.Private) > 0) acc = features.privateKey; bool isStatic = (flags & FlagType.Static) > 0; if (isStatic) acc = features.staticKey + " " + acc; if (!isAS2Event && !isObjectMethod) acc = features.overrideKey + " " + acc; if ((flags & (FlagType.Getter | FlagType.Setter)) > 0) { string type = member.Type; string name = member.Name; if (member.Parameters != null && member.Parameters.Count > 0) type = member.Parameters[0].Type; type = FormatType(type); if (type == null) { string message = String.Format(TextHelper.GetString("Info.TypeDeclMissing"), member.Name); ErrorManager.ShowInfo(message); return; } if (ofClass.Members.Search(name, FlagType.Getter, 0) != null) { decl += String.Format(GetTemplate("Getter"), acc, name, type, "super." + name); } if (ofClass.Members.Search(name, FlagType.Setter, 0) != null) { string tpl = GetTemplate("Setter"); if (decl.Length > 0) { decl += "\n\n"; tpl = tpl.Replace("$(EntryPoint)", ""); } decl += String.Format(tpl, acc, name, type, "super." + name, ASContext.Context.Features.voidKey ?? "void"); } } else { string type = FormatType(member.Type); if (type == null) { string message = String.Format(TextHelper.GetString("Info.TypeDeclMissing"), member.Name); ErrorManager.ShowInfo(message); return; } if (acc.Length > 0) acc += " "; decl = acc + features.functionKey + " "; bool noRet = type.Equals("void", StringComparison.OrdinalIgnoreCase); type = (noRet) ? ASContext.Context.Features.voidKey : type; if (!noRet) typesUsed.Add(getQualifiedType(type, ofClass)); string action = (isProxy || isAS2Event) ? "" : GetSuperCall(member, typesUsed, ofClass); decl += member.Name + String.Format(GetTemplate("MethodOverride"), member.ParametersString(true), type, action); } Sci.BeginUndoAction(); try { if (ASContext.Context.Settings.GenerateImports && typesUsed.Count > 0) { int offset = AddImportsByName(typesUsed, line); position += offset; startPos += offset; } Sci.SetSel(startPos, position + member.Name.Length); InsertCode(startPos, decl); } finally { Sci.EndUndoAction(); } }
private static void AddInterfaceDefJob(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member) { ClassModel aType = ASContext.Context.ResolveType(contextParam, ASContext.Context.CurrentModel); if (aType.IsVoid()) return; FileModel fileModel = ASFileParser.ParseFile(aType.InFile.FileName, ASContext.Context); foreach (ClassModel cm in fileModel.Classes) { if (cm.QualifiedName.Equals(aType.QualifiedName)) { aType = cm; break; } } StringBuilder snippet = new StringBuilder(); DockContent dc = ASContext.MainForm.OpenEditableDocument(aType.InFile.FileName, true); Sci = ASContext.CurSciControl; MemberModel latest = FindLatest(FlagType.Function, aType); int position; if (latest == null) { position = GetBodyStart(aType.LineFrom, aType.LineTo, Sci); } else { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 1 : 0); snippet.Append(NewLine); } Sci.SetSel(position, position); Sci.CurrentPos = position; List<MemberModel> parms = member.Parameters; snippet.Append("function ") .Append(member.ToDeclarationString()) .Append(";"); List<string> importsList = new List<string>(); string t; if (parms != null && parms.Count > 0) { for (int i = 0; i < parms.Count; i++) { if (parms[i].Type != null) { t = getQualifiedType(parms[i].Type, inClass); importsList.Add(t); } } } if (member.Type != null) { t = getQualifiedType(member.Type, inClass); importsList.Add(t); } if (importsList.Count > 0) { int o = AddImportsByName(importsList, Sci.LineFromPosition(position)); position += o; Sci.SetSel(position, position); } InsertCode(position, snippet.ToString()); Sci.CurrentPos = position; }
private static void GenerateFieldFromParameter(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass) { int funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci); Sci.SetSel(funcBodyStart, Sci.LineEndPosition(member.LineTo)); string body = Sci.SelText; string trimmed = body.TrimStart(); Regex re = new Regex("^super\\s*[\\(|\\.]"); Match m = re.Match(trimmed); if (m.Success) { if (m.Index == 0) { int p = funcBodyStart + (body.Length - trimmed.Length); int l = Sci.LineFromPosition(p); p = Sci.PositionFromLine(l + 1); funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci, p); } } Sci.SetSel(funcBodyStart, funcBodyStart); Sci.CurrentPos = funcBodyStart; bool isVararg = false; string paramName = contextMember.Name; if (paramName.StartsWith("...")) { paramName = paramName.TrimStart(new char[] { ' ', '.' }); isVararg = true; } string varName = paramName; bool addThis = true; if (ASContext.CommonSettings.PrefixFields.Length > 0 && !paramName.StartsWith(ASContext.CommonSettings.PrefixFields)) { varName = ASContext.CommonSettings.PrefixFields + varName; addThis = ASContext.CommonSettings.GenerateScope; } StringBuilder sb = new StringBuilder(); if (addThis) sb.Append("this."); sb.Append(varName) .Append(" = ") .Append(paramName) .Append(";") .Append(NewLine) .Append("$(Boundary)"); SnippetHelper.InsertSnippetText(Sci, funcBodyStart, sb.ToString()); MemberList classMembers = inClass.Members; foreach (MemberModel classMember in classMembers) { if (classMember.Name.Equals(varName)) { return; } } MemberModel latest = FindLatest(FlagType.Variable | FlagType.Constant, GetDefaultVisibility(), inClass); if (latest == null) return; int position = FindNewVarPosition(Sci, inClass, latest); if (position <= 0) return; Sci.SetSel(position, position); Sci.CurrentPos = position; MemberModel mem = NewMember(varName, member, FlagType.Variable, GetDefaultVisibility()); if (isVararg) { mem.Type = "Array"; } else { mem.Type = contextMember.Type; } GenerateVariable(mem, position, true); ASContext.Panel.RestoreLastLookupPosition(); }
private static void ChangeMethodDecl(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass) { int wordPos = Sci.WordEndPosition(Sci.CurrentPos, true); List<FunctionParameter> functionParameters = ParseFunctionParameters(Sci, wordPos); ASResult funcResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true)); if (funcResult.Member == null) { return; } if (funcResult != null && funcResult.inClass != null && !funcResult.inClass.Equals(inClass)) { AddLookupPosition(); lookupPosition = -1; DockContent dc = ASContext.MainForm.OpenEditableDocument(funcResult.inClass.InFile.FileName, true); Sci = ASContext.CurSciControl; FileModel fileModel = new FileModel(); ASFileParser parser = new ASFileParser(); parser.ParseSrc(fileModel, Sci.Text); foreach (ClassModel cm in fileModel.Classes) { if (cm.QualifiedName.Equals(funcResult.inClass.QualifiedName)) { funcResult.inClass = cm; break; } } inClass = funcResult.inClass; } MemberList members = inClass.Members; foreach (MemberModel m in members) { if (m.Name == funcResult.Member.Name) { funcResult.Member = m; } } bool paramsDiffer = false; if (funcResult.Member.Parameters != null) { // check that parameters have one and the same type if (funcResult.Member.Parameters.Count == functionParameters.Count) { if (functionParameters.Count > 0) { List<MemberModel> parameters = funcResult.Member.Parameters; for (int i = 0; i < parameters.Count; i++) { MemberModel p = parameters[i]; if (p.Type != functionParameters[i].paramType) { paramsDiffer = true; break; } } } } else { paramsDiffer = true; } } // check that parameters count differs else if (functionParameters.Count != 0) { paramsDiffer = true; } if (paramsDiffer) { int app = 0; List<MemberModel> newParameters = new List<MemberModel>(); List<MemberModel> existingParameters = funcResult.Member.Parameters; for (int i = 0; i < functionParameters.Count; i++) { FunctionParameter p = functionParameters[i]; if (existingParameters != null && existingParameters.Count > (i - app) && existingParameters[i - app].Type == p.paramType) { newParameters.Add(existingParameters[i - app]); } else { if (existingParameters != null && existingParameters.Count < functionParameters.Count) { app++; } newParameters.Add(new MemberModel(p.param, p.paramType, FlagType.ParameterVar, 0)); } } funcResult.Member.Parameters = newParameters; int posStart = Sci.PositionFromLine(funcResult.Member.LineFrom); int posEnd = Sci.LineEndPosition(funcResult.Member.LineTo); Sci.SetSel(posStart, posEnd); string selectedText = Sci.SelText; Regex rStart = new Regex(String.Format(@"\s+{0}\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?", funcResult.Member.Name)); Match mStart = rStart.Match(selectedText); if (!mStart.Success) { return; } int start = mStart.Index + posStart; int end = start + mStart.Length; Sci.SetSel(start, end); string decl = funcResult.Member.ToDeclarationString(); Sci.ReplaceSel(" " + decl); // add imports to function argument types if (functionParameters.Count > 0) { List<string> l = new List<string>(); foreach (FunctionParameter fp in functionParameters) { try { l.Add(fp.paramQualType); } catch (Exception) { } } start += AddImportsByName(l, Sci.LineFromPosition(end)); } Sci.SetSel(start, start); } }
private static void GenerateToString(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member) { MemberModel resultMember = new MemberModel("toString", "String", FlagType.Function, Visibility.Public); bool isOverride = false; inClass.ResolveExtends(); if (inClass.Extends != null) { ClassModel aType = inClass.Extends; while (!aType.IsVoid() && aType.QualifiedName != "Object") { foreach (MemberModel method in aType.Members) { if (method.Name == "toString") { isOverride = true; break; } } if (isOverride) { resultMember.Flags |= FlagType.Override; break; } // interface inheritance aType = aType.Extends; } } MemberList members = inClass.Members; StringBuilder membersString = new StringBuilder(); StringBuilder oneMembersString; int len = 0; int indent = Sci.GetLineIndentation(Sci.LineFromPosition(Sci.CurrentPos)); foreach (MemberModel m in members) { if (((m.Flags & FlagType.Variable) > 0 || (m.Flags & FlagType.Getter) > 0) && (m.Access & Visibility.Public) > 0 && (m.Flags & FlagType.Constant) == 0) { oneMembersString = new StringBuilder(); oneMembersString.Append(" ").Append(m.Name).Append("=\" + ").Append(m.Name).Append(" + "); membersString.Append(oneMembersString); len += oneMembersString.Length; if (len > 80) { len = 0; membersString.Append("\n\t\t\t\t"); } membersString.Append("\""); } } string template = TemplateUtils.GetTemplate("ToString"); string result = TemplateUtils.ToDeclarationWithModifiersString(resultMember, template); result = TemplateUtils.ReplaceTemplateVariable(result, "Body", "\"[" + inClass.Name + membersString.ToString() + "]\""); InsertCode(Sci.CurrentPos, result); }
private static void AddAsParameter(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member, bool detach) { if (!RemoveLocalDeclaration(Sci, contextMember)) return; int currLine = Sci.LineFromPosition(Sci.CurrentPos); int posStart = Sci.PositionFromLine(member.LineFrom); int posEnd = Sci.LineEndPosition(member.LineTo); Sci.SetSel(posStart, posEnd); string selectedText = Sci.SelText; Regex rStart = new Regex(@"\s{1}" + member.Name + @"\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?"); Match mStart = rStart.Match(selectedText); if (!mStart.Success) return; int start = mStart.Index + posStart + 1; int end = mStart.Index + posStart + mStart.Length; Sci.SetSel(start, end); MemberModel memberCopy = (MemberModel) member.Clone(); if (memberCopy.Parameters == null) memberCopy.Parameters = new List<MemberModel>(); memberCopy.Parameters.Add(contextMember); string template = TemplateUtils.ToDeclarationString(memberCopy, TemplateUtils.GetTemplate("MethodDeclaration")); InsertCode(start, template); int currPos = Sci.LineEndPosition(currLine); Sci.SetSel(currPos, currPos); Sci.CurrentPos = currPos; }
public override bool OnCompletionInsert(ScintillaNet.ScintillaControl sci, int position, string text, char trigger) { if (text == "Dictionary") { string insert = null; string line = sci.GetLine(sci.LineFromPosition(position)); Match m = Regex.Match(line, @"\svar\s+(?<varname>.+)\s*:\s*Dictionary\.<(?<indextype>.+)(?=(>\s*=))"); if (m.Success) { insert = String.Format(".<{0}>", m.Groups["indextype"].Value); } else { m = Regex.Match(line, @"\s*=\s*new"); if (m.Success) { ASResult result = ASComplete.GetExpressionType(sci, sci.PositionFromLine(sci.LineFromPosition(position)) + m.Index); if (result != null && !result.IsNull() && result.Member != null && result.Member.Type != null) { m = Regex.Match(result.Member.Type, @"(?<=<).+(?=>)"); if (m.Success) { insert = String.Format(".<{0}>", m.Value); } } } if (insert == null) { if (trigger == '.' || trigger == '(') return true; insert = ".<>"; sci.InsertText(position + text.Length, insert); sci.CurrentPos = position + text.Length + 2; sci.SetSel(sci.CurrentPos, sci.CurrentPos); ASComplete.HandleAllClassesCompletion(sci, "", false, true); return true; } } if (insert == null) return false; if (trigger == '.') { sci.InsertText(position + text.Length, insert.Substring(1)); sci.CurrentPos = position + text.Length; } else { sci.InsertText(position + text.Length, insert); sci.CurrentPos = position + text.Length + insert.Length; } sci.SetSel(sci.CurrentPos, sci.CurrentPos); return true; } return base.OnCompletionInsert(sci, position, text, trigger); }
public static void GenerateDelegateMethods(ScintillaNet.ScintillaControl Sci, MemberModel member, Dictionary<MemberModel, ClassModel> selectedMembers, ClassModel classModel, ClassModel inClass) { Sci.BeginUndoAction(); try { StringBuilder sb = new StringBuilder( String.Format(GetTemplate("DelegateMethodsHeader", "$(Boundary)\n\n/* DELEGATE {0} */"), classModel.Type)); MemberModel latest; int position = -1; List<MemberModel> methodParams; ClassModel type; List<string> importsList = new List<string>(); bool isStaticMember = false; if ((member.Flags & FlagType.Static) > 0) { isStaticMember = true; } inClass.ResolveExtends(); Dictionary<MemberModel, ClassModel>.KeyCollection selectedMemberKeys = selectedMembers.Keys; foreach (MemberModel m in selectedMemberKeys) { sb.Append("$(Boundary)\n\n"); bool overrideFound = false; ClassModel aType = inClass; while (aType != null && !aType.IsVoid()) { MemberList inClassMembers = aType.Members; foreach (MemberModel inClassMember in inClassMembers) { if ((inClassMember.Flags & FlagType.Function) > 0 && m.Name.Equals(inClassMember.Name)) { sb.Append("override "); overrideFound = true; break; } } if (overrideFound) { break; } aType = aType.Extends; } if (isStaticMember) { sb.Append("static "); } sb.Append(GetDeclaration(m)) .Append(" $(CSLB){\n\t"); if (m.Type != null && m.Type.ToLower() != "void") { sb.Append("return "); } methodParams = m.Parameters; // check for varargs bool isVararg = false; if (methodParams != null && methodParams.Count > 0) { MemberModel mm = methodParams[methodParams.Count - 1]; if (mm.Name.StartsWith("...")) { isVararg = true; } } if (!isVararg) { sb.Append(member.Name) .Append(".") .Append(m.Name) .Append("("); if (methodParams != null) { for (int i = 0; i < methodParams.Count; i++) { MemberModel param = methodParams[i]; sb.Append(param.Name); if (i + 1 < methodParams.Count) { sb.Append(", "); } } } sb.Append(");"); } else { sb.Append(member.Name) .Append(".") .Append(m.Name) .Append(".apply(null, ["); for (int i = 0; i < methodParams.Count; i++) { MemberModel param = methodParams[i]; if (i + 1 < methodParams.Count) { sb.Append(param.Name); if (i + 2 < methodParams.Count) { sb.Append(", "); } } else { sb.Append("].concat(") .Append(param.Name.Substring(3)) .Append(")"); } } sb.Append(");"); } sb.Append("\n}"); if (methodParams != null) { for (int i = 0; i < methodParams.Count; i++) { MemberModel param = methodParams[i]; if (param.Type != null) { type = ASContext.Context.ResolveType(param.Type, selectedMembers[m].InFile); importsList.Add(type.QualifiedName); } } } if (position < 0) { latest = FindLatest(FlagType.Function, inClass); if (latest == null) { position = Sci.WordStartPosition(Sci.CurrentPos, true); Sci.SetSel(position, Sci.WordEndPosition(position, true)); } else { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); Sci.SetSel(position, position); } } else { position = Sci.CurrentPos; } if (m.Type != null) { type = ASContext.Context.ResolveType(m.Type, selectedMembers[m].InFile); importsList.Add(type.QualifiedName); } } if (importsList.Count > 0 && position > -1) { int o = AddImportsByName(importsList, Sci.LineFromPosition(position)); position += o; Sci.SetSel(position, position); } InsertCode(position, sb.ToString()); } finally { Sci.EndUndoAction(); } }
public bool IsMultilineSelection(ScintillaNet.ScintillaControl sci) { return sci.LineFromPosition(sci.SelectionStart) != sci.LineFromPosition(sci.SelectionEnd); }
static public void ContextualGenerator(ScintillaNet.ScintillaControl Sci) { if (ASContext.Context is ASContext) (ASContext.Context as ASContext).UpdateCurrentFile(false); // update model lookupPosition = -1; int position = Sci.CurrentPos; if (Sci.BaseStyleAt(position) == 19) // on keyword return; int line = Sci.LineFromPosition(position); contextToken = Sci.GetWordFromPosition(position); contextMatch = null; FoundDeclaration found = GetDeclarationAtLine(Sci, line); string text = Sci.GetLine(line); bool suggestItemDeclaration = false; if (!String.IsNullOrEmpty(contextToken) && Char.IsDigit(contextToken[0])) { ShowConvertToConst(found); return; } ASResult resolve = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(position, true)); // ignore automatic vars (MovieClip members) if (resolve.Member != null && (((resolve.Member.Flags & FlagType.AutomaticVar) > 0) || (resolve.inClass != null && resolve.inClass.QualifiedName == "Object"))) { resolve.Member = null; resolve.Type = null; } if (found.inClass != ClassModel.VoidClass && contextToken != null) { if (resolve.Member == null && resolve.Type != null && (resolve.Type.Flags & FlagType.Interface) > 0) // implement interface { contextParam = resolve.Type.Type; ShowImplementInterface(found); return; } if (resolve.Member != null && !ASContext.Context.CurrentClass.IsVoid() && (resolve.Member.Flags & FlagType.LocalVar) > 0) // promote to class var { contextMember = resolve.Member; ShowPromoteLocalAndAddParameter(found); return; } if (resolve.Member == null && resolve.Type == null) // import declaration { if (CheckAutoImport(found)) { return; } else { int stylemask = (1 << Sci.StyleBits) - 1; if (ASComplete.IsTextStyle(Sci.StyleAt(position - 1) & stylemask)) { suggestItemDeclaration = true; } } } } if (found.member != null) { // private var -> property if ((found.member.Flags & FlagType.Variable) > 0 && (found.member.Flags & FlagType.LocalVar) == 0) { // maybe we just want to import the member's non-imported type Match m = Regex.Match(text, String.Format(patternVarDecl, found.member.Name, contextToken)); if (m.Success) { contextMatch = m; ClassModel type = ASContext.Context.ResolveType(contextToken, ASContext.Context.CurrentModel); if (type.IsVoid() && CheckAutoImport(found)) return; } // create property ShowGetSetList(found); return; } // inside a function else if ((found.member.Flags & (FlagType.Function | FlagType.Getter | FlagType.Setter)) > 0 && resolve.Member == null && resolve.Type == null) { if (contextToken != null) { // "generate event handlers" suggestion Match m = Regex.Match(text, String.Format(patternEvent, contextToken), RegexOptions.IgnoreCase); if (m.Success) { contextMatch = m; contextParam = CheckEventType(m.Groups["event"].Value); ShowEventList(found); return; } m = Regex.Match(text, String.Format(patternDelegate, contextToken), RegexOptions.IgnoreCase); if (m.Success) { contextMatch = m; ShowDelegateList(found); return; } } } // "Generate fields from parameters" suggestion if (found.member != null && (found.member.Flags & FlagType.Function) > 0 && (found.member.Flags & FlagType.Static) == 0 && found.member.Parameters != null && (found.member.Parameters.Count > 0) && resolve.Member != null && (resolve.Member.Flags & FlagType.ParameterVar) > 0) { contextMember = resolve.Member; ShowFieldFromParameter(found); return; } // "add to interface" suggestion if (resolve.Member != null && resolve.Member.Name == found.member.Name && (found.member.Flags & FlagType.Function) > 0 && found.inClass != null && found.inClass.Implements != null && found.inClass.Implements.Count > 0) { string funcName = found.member.Name; int classPosStart = Sci.PositionFromLine(found.inClass.LineFrom); bool skip = false; foreach (string interf in found.inClass.Implements) { if (skip) { break; } ClassModel cm = ASContext.Context.ResolveType(interf, ASContext.Context.CurrentModel); contextParam = cm.Type; MemberList members = cm.Members; foreach (MemberModel m in members) { if (m.Name.Equals(funcName)) { skip = true; break; } } } if (!skip && contextParam != null) { ShowAddInterfaceDefList(found); return; } } // "assign var to statement" siggestion int curLine = Sci.LineFromPosition(Sci.CurrentPos); string ln = Sci.GetLine(curLine); if (ln.Trim().Length > 0 && ln.TrimEnd().Length <= Sci.CurrentPos - Sci.PositionFromLine(curLine)) { Regex re = new Regex("="); Match m = re.Match(ln); if (!m.Success) { ShowAssignStatementToVarList(found); } } } // suggest generate constructor / toString if (found.member == null && found.inClass != ClassModel.VoidClass && contextToken == null) { ClassModel cm = ASContext.Context.CurrentClass; MemberList members = cm.Members; bool hasConstructor = false; bool hasToString = false; foreach (MemberModel m in members) { if ((m.Flags & FlagType.Constructor) > 0) { hasConstructor = true; } if ((m.Flags & FlagType.Function) > 0 && m.Name.Equals("toString")) { hasToString = true; } } if (!hasConstructor || !hasToString) { ShowConstructorAndToStringList(found, hasConstructor, hasToString); return; } } if (resolve.Member != null && resolve.Type != null && resolve.Type.QualifiedName == "String" && found.inClass != null) { int lineStartPos = Sci.PositionFromLine(Sci.LineFromPosition(Sci.CurrentPos)); string lineStart = text.Substring(0, Sci.CurrentPos - lineStartPos); Match m = Regex.Match(lineStart, String.Format(@"new\s+(?<event>\w+)\s*\(\s*\w+", lineStart)); if (m.Success) { Group g = m.Groups["event"]; ASResult eventResolve = ASComplete.GetExpressionType(Sci, lineStartPos + g.Index + g.Length); if (eventResolve != null && eventResolve.Type != null) { ClassModel aType = eventResolve.Type; aType.ResolveExtends(); while (!aType.IsVoid() && aType.QualifiedName != "Object") { if (aType.QualifiedName == "flash.events.Event") { contextParam = eventResolve.Type.QualifiedName; ShowEventMetatagList(found); return; } aType = aType.Extends; } } } } // suggest declaration if (contextToken != null) { if (suggestItemDeclaration) { Match m = Regex.Match(text, String.Format(patternClass, contextToken)); if (m.Success) { contextMatch = m; ShowNewClassList(found); } else { m = Regex.Match(text, String.Format(patternMethod, contextToken)); if (m.Success) { contextMatch = m; ShowNewMethodList(found); } else ShowNewVarList(found); } } else { if (resolve != null && resolve.inClass != null && resolve.inClass.InFile != null && resolve.Member != null && (resolve.Member.Flags & FlagType.Function) > 0 && File.Exists(resolve.inClass.InFile.FileName) && !resolve.inClass.InFile.FileName.StartsWith(PathHelper.AppDir)) { Match m = Regex.Match(text, String.Format(patternMethodDecl, contextToken)); Match m2 = Regex.Match(text, String.Format(patternMethod, contextToken)); if (!m.Success && m2.Success) { contextMatch = m; ShowChangeMethodDeclList(found); } } } } // TODO Empty line, show generators list? }
private static void ChangeDecl(ScintillaNet.ScintillaControl Sci, MemberModel memberModel, List<FunctionParameter> functionParameters) { bool paramsDiffer = false; if (memberModel.Parameters != null) { // check that parameters have one and the same type if (memberModel.Parameters.Count == functionParameters.Count) { if (functionParameters.Count > 0) { List<MemberModel> parameters = memberModel.Parameters; for (int i = 0; i < parameters.Count; i++) { MemberModel p = parameters[i]; if (p.Type != functionParameters[i].paramType) { paramsDiffer = true; break; } } } } else { paramsDiffer = true; } } // check that parameters count differs else if (functionParameters.Count != 0) { paramsDiffer = true; } if (paramsDiffer) { int app = 0; List<MemberModel> newParameters = new List<MemberModel>(); List<MemberModel> existingParameters = memberModel.Parameters; for (int i = 0; i < functionParameters.Count; i++) { FunctionParameter p = functionParameters[i]; if (existingParameters != null && existingParameters.Count > (i - app) && existingParameters[i - app].Type == p.paramType) { newParameters.Add(existingParameters[i - app]); } else { if (existingParameters != null && existingParameters.Count < functionParameters.Count) { app++; } newParameters.Add(new MemberModel(p.paramName, p.paramType, FlagType.ParameterVar, 0)); } } memberModel.Parameters = newParameters; int posStart = Sci.PositionFromLine(memberModel.LineFrom); int posEnd = Sci.LineEndPosition(memberModel.LineTo); Sci.SetSel(posStart, posEnd); string selectedText = Sci.SelText; Regex rStart = new Regex(@"\s{1}" + memberModel.Name + @"\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?"); Match mStart = rStart.Match(selectedText); if (!mStart.Success) { return; } int start = mStart.Index + posStart; int end = start + mStart.Length; Sci.SetSel(start, end); string decl = TemplateUtils.ToDeclarationString(memberModel, TemplateUtils.GetTemplate("MethodDeclaration")); InsertCode(Sci.CurrentPos, "$(Boundary) " + decl); // add imports to function argument types if (functionParameters.Count > 0) { List<string> l = new List<string>(); foreach (FunctionParameter fp in functionParameters) { try { l.Add(fp.paramQualType); } catch (Exception) { } } start += AddImportsByName(l, Sci.LineFromPosition(end)); } Sci.SetSel(start, start); } }
private static void AssignStatementToVar(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member) { int lineNum = Sci.LineFromPosition(Sci.CurrentPos); string line = Sci.GetLine(lineNum); StatementReturnType returnType = GetStatementReturnType(Sci, inClass, line, Sci.PositionFromLine(lineNum)); if (returnType == null) { return; } IASContext cntx = inClass.InFile.Context; bool isAs3 = cntx.Settings.LanguageId == "AS3"; string voidWord = isAs3 ? "void" : "Void"; string type = null; string varname = null; string cleanType = null; ASResult resolve = returnType.resolve; int pos = returnType.position; string word = returnType.word; if (resolve != null && !resolve.IsNull()) { if (resolve.Member != null && resolve.Member.Type != null) { type = resolve.Member.Type; } else if (resolve.Type != null && resolve.Type.Name != null) { type = returnType.resolve.Type.QualifiedName; } if (resolve.Member != null && resolve.Member.Name != null) { varname = GuessVarName(resolve.Member.Name, type); } } if (word != null && Char.IsDigit(word[0])) { word = null; } if (type == voidWord) { type = null; } if (varname == null) { varname = GuessVarName(word, type); } if (varname != null && varname == word) { if (varname.Length == 1) { varname = varname + "1"; } else { varname = varname[0] + ""; } } if (type != null) { cleanType = FormatType(GetShortType(type)); } StringBuilder sb = new StringBuilder(); sb.Append("var "); if (varname != null) { sb.Append(varname); sb.Append(":"); } if (cleanType != null) { sb.Append(cleanType); } else { sb.Append("$(EntryPoint)"); } sb.Append(" = $(Boundary)"); int indent = Sci.GetLineIndentation(lineNum); pos = Sci.PositionFromLine(lineNum) + indent / Sci.Indent; Sci.CurrentPos = pos; Sci.SetSel(pos, pos); InsertCode(pos, sb.ToString()); if (type != null) { ClassModel inClassForImport = null; if (resolve.inClass != null) { inClassForImport = resolve.inClass; } else if (resolve.relClass != null) { inClassForImport = resolve.relClass; } else { inClassForImport = inClass; } List<string> l = new List<string>(); l.Add(getQualifiedType(type, inClassForImport)); pos += AddImportsByName(l, Sci.LineFromPosition(pos)); } }
private static void GenerateFieldFromParameter(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass, Visibility scope) { int funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci); Sci.SetSel(funcBodyStart, Sci.LineEndPosition(member.LineTo)); string body = Sci.SelText; string trimmed = body.TrimStart(); Regex re = new Regex("^super\\s*[\\(|\\.]"); Match m = re.Match(trimmed); if (m.Success) { if (m.Index == 0) { int p = funcBodyStart + (body.Length - trimmed.Length); int l = Sci.LineFromPosition(p); p = Sci.PositionFromLine(l + 1); funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci, p); } } Sci.SetSel(funcBodyStart, funcBodyStart); Sci.CurrentPos = funcBodyStart; bool isVararg = false; string paramName = contextMember.Name; if (paramName.StartsWith("...")) { paramName = paramName.TrimStart(new char[] { ' ', '.' }); isVararg = true; } string varName = paramName; string scopedVarName = varName; if ((scope & Visibility.Public) > 0) { if ((member.Flags & FlagType.Static) > 0) scopedVarName = inClass.Name + "." + varName; else scopedVarName = "this." + varName; } else { if (ASContext.CommonSettings.PrefixFields.Length > 0 && !paramName.StartsWith(ASContext.CommonSettings.PrefixFields)) { scopedVarName = varName = ASContext.CommonSettings.PrefixFields + varName; } if (ASContext.CommonSettings.GenerateScope || ASContext.CommonSettings.PrefixFields == "") { if ((member.Flags & FlagType.Static) > 0) scopedVarName = inClass.Name + "." + varName; else scopedVarName = "this." + varName; } } string template = TemplateUtils.GetTemplate("FieldFromParameter"); template = TemplateUtils.ReplaceTemplateVariable(template, "Name", scopedVarName); template = TemplateUtils.ReplaceTemplateVariable(template, "Value", paramName); template += "\n$(Boundary)"; SnippetHelper.InsertSnippetText(Sci, funcBodyStart, template); MemberList classMembers = inClass.Members; foreach (MemberModel classMember in classMembers) if (classMember.Name.Equals(varName)) return; MemberModel latest = GetLatestMemberForVariable(GeneratorJobType.Variable, inClass, GetDefaultVisibility(), new MemberModel()); if (latest == null) return; int position = FindNewVarPosition(Sci, inClass, latest); if (position <= 0) return; Sci.SetSel(position, position); Sci.CurrentPos = position; MemberModel mem = NewMember(varName, member, FlagType.Variable, scope); if (isVararg) mem.Type = "Array"; else mem.Type = contextMember.Type; GenerateVariable(mem, position, true); ASContext.Panel.RestoreLastLookupPosition(); }
public void GotoPosAndFocus(ScintillaNet.ScintillaControl sci, int position) { sci.MBSafeGotoPos(position); int line = sci.LineFromPosition(sci.CurrentPos); // TODO The folding makes the line centering fail - solution: expand all folds / make it smarter. //sci.EnsureVisible(line); sci.ExpandAllFolds(); int top = sci.FirstVisibleLine; int middle = top + sci.LinesOnScreen/2; sci.LineScroll(0, line-middle); // ((Control)sci).Focus(); }
private static void GenerateVariableJob(GeneratorJobType job, ScintillaNet.ScintillaControl Sci, MemberModel member, bool detach, ClassModel inClass) { int position = 0; MemberModel latest = null; bool isOtherClass = false; Visibility varVisi = job.Equals(GeneratorJobType.Variable) ? GetDefaultVisibility() : Visibility.Public; FlagType ft = job.Equals(GeneratorJobType.Constant) ? FlagType.Constant : FlagType.Variable; // evaluate, if the variable (or constant) should be generated in other class ASResult varResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true)); int contextOwnerPos = GetContextOwnerEndPos(Sci, Sci.WordStartPosition(Sci.CurrentPos, true)); MemberModel isStatic = new MemberModel(); if (contextOwnerPos != -1) { ASResult contextOwnerResult = ASComplete.GetExpressionType(Sci, contextOwnerPos); if (contextOwnerResult != null) { if (contextOwnerResult.Member == null && contextOwnerResult.Type != null) { isStatic.Flags |= FlagType.Static; } } } else if (member != null && (member.Flags & FlagType.Static) > 0) { isStatic.Flags |= FlagType.Static; } ASResult returnType = null; int lineNum = Sci.CurrentLine; string line = Sci.GetLine(lineNum); Match m = Regex.Match(line, "\\b" + Regex.Escape(contextToken) + "\\("); if (m.Success) { returnType = new ASResult(); returnType.Type = ASContext.Context.ResolveType("Function", null); } else { m = Regex.Match(line, @"=\s*[^;\n\r}}]+"); if (m.Success) { int posLineStart = Sci.PositionFromLine(lineNum); if (posLineStart + m.Index >= Sci.CurrentPos) { line = line.Substring(m.Index); StatementReturnType rType = GetStatementReturnType(Sci, inClass, line, posLineStart + m.Index); if (rType != null) { returnType = rType.resolve; } } } } if (varResult.RelClass != null && !varResult.RelClass.IsVoid() && !varResult.RelClass.Equals(inClass)) { AddLookupPosition(); lookupPosition = -1; ASContext.MainForm.OpenEditableDocument(varResult.RelClass.InFile.FileName, false); Sci = ASContext.CurSciControl; isOtherClass = true; FileModel fileModel = new FileModel(); fileModel.Context = ASContext.Context; ASFileParser parser = new ASFileParser(); parser.ParseSrc(fileModel, Sci.Text); foreach (ClassModel cm in fileModel.Classes) { if (cm.QualifiedName.Equals(varResult.RelClass.QualifiedName)) { varResult.RelClass = cm; break; } } inClass = varResult.RelClass; ASContext.Context.UpdateContext(inClass.LineFrom); } latest = GetLatestMemberForVariable(job, inClass, varVisi, isStatic); // if we generate variable in current class.. if (!isOtherClass && member == null) { detach = false; lookupPosition = -1; position = Sci.WordStartPosition(Sci.CurrentPos, true); Sci.SetSel(position, Sci.WordEndPosition(position, true)); } else // if we generate variable in another class { if (latest != null) { position = FindNewVarPosition(Sci, inClass, latest); } else { position = GetBodyStart(inClass.LineFrom, inClass.LineTo, Sci); detach = false; } if (position <= 0) return; Sci.SetSel(position, position); } // if this is a constant, we assign a value to constant string returnTypeStr = null; string eventValue = null; if (job == GeneratorJobType.Constant && returnType == null) { isStatic.Flags |= FlagType.Static; eventValue = "String = \"" + Camelize(contextToken) + "\""; } else if (returnType != null) { ClassModel inClassForImport = null; if (returnType.InClass != null) { inClassForImport = returnType.InClass; } else if (returnType.RelClass != null) { inClassForImport = returnType.RelClass; } else { inClassForImport = inClass; } List<String> imports = new List<string>(); if (returnType.Member != null) { if (returnType.Member.Type != ASContext.Context.Features.voidKey) { returnTypeStr = FormatType(GetShortType(returnType.Member.Type)); imports.Add(getQualifiedType(returnType.Member.Type, inClassForImport)); } } else if (returnType != null && returnType.Type != null) { returnTypeStr = FormatType(GetShortType(returnType.Type.QualifiedName)); imports.Add(getQualifiedType(returnType.Type.QualifiedName, inClassForImport)); } if (imports.Count > 0) { position += AddImportsByName(imports, Sci.LineFromPosition(position)); Sci.SetSel(position, position); } } MemberModel newMember = NewMember(contextToken, isStatic, ft, varVisi); if (returnTypeStr != null) { newMember.Type = returnTypeStr; } else if (eventValue != null) { newMember.Type = eventValue; } GenerateVariable(newMember, position, detach); }
/// <summary> /// Track text modifications /// </summary> public virtual void TrackTextChange(ScintillaNet.ScintillaControl sender, int position, int length, int linesAdded) { if (cFile != FileModel.Ignore && !cFile.OutOfDate) { // test: modifications inside function body if (linesAdded == 0 && cMember != null && (cMember.Flags & FlagType.Function) > 0) { // TODO More precise text modifications tracking int line = sender.LineFromPosition(position); if (line > cMember.LineFrom && line <= cMember.LineTo) { // file AS3 in MXML ranges if (cFile.InlinedRanges != null) { foreach (InlineRange range in cFile.InlinedRanges) { if (range.Start > position) range.Start += length; if (range.End > position) range.End += length; } } return; } /*string fbody = ""; for (int i = cMember.LineFrom; i <= cMember.LineTo; i++) { fbody += sci.GetLine(i); } position -= sci.PositionFromLine(cMember.LineFrom); if (fbody.IndexOf('{') < position) return;*/ } cFile.OutOfDate = true; } }
public static void GenerateExtractMethod(ScintillaNet.ScintillaControl Sci, string NewName) { FileModel cFile; IASContext context = ASContext.Context; string selection = Sci.SelText; if (selection == null || selection.Length == 0) { return; } if (selection.TrimStart().Length == 0) { return; } Sci.SetSel(Sci.SelectionStart + selection.Length - selection.TrimStart().Length, Sci.SelectionEnd); Sci.CurrentPos = Sci.SelectionEnd; Int32 pos = Sci.CurrentPos; int lineStart = Sci.LineFromPosition(Sci.SelectionStart); int lineEnd = Sci.LineFromPosition(Sci.SelectionEnd); int firstLineIndent = Sci.GetLineIndentation(lineStart); int entryPointIndent = Sci.Indent; for (int i = lineStart; i <= lineEnd; i++) { int indent = Sci.GetLineIndentation(i); if (i > lineStart) { Sci.SetLineIndentation(i, indent - firstLineIndent + entryPointIndent); } } string selText = Sci.SelText; string template = TemplateUtils.GetTemplate("CallFunction"); template = TemplateUtils.ReplaceTemplateVariable(template, "Name", NewName); template = TemplateUtils.ReplaceTemplateVariable(template, "Arguments", ""); ASGenerator.InsertCode(Sci.CurrentPos, template + ";"); cFile = ASContext.Context.CurrentModel; ASFileParser parser = new ASFileParser(); parser.ParseSrc(cFile, Sci.Text); bool isAs3 = cFile.Context.Settings.LanguageId == "AS3"; FoundDeclaration found = GetDeclarationAtLine(Sci, lineStart); if (found == null || found.member == null) { return; } lookupPosition = Sci.CurrentPos; AddLookupPosition(); MemberModel latest = TemplateUtils.GetTemplateBlockMember(Sci, TemplateUtils.GetBoundary("PrivateMethods")); if (latest == null) latest = GetLatestMemberForFunction(found.inClass, ASGenerator.GetDefaultVisibility(), found.member); if (latest == null) latest = found.member; int position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); Sci.SetSel(position, position); FlagType flags = FlagType.Function; if ((found.member.Flags & FlagType.Static) > 0) { flags |= FlagType.Static; } MemberModel m = new MemberModel(NewName, context.Features.voidKey, flags, ASGenerator.GetDefaultVisibility()); template = NewLine + TemplateUtils.GetTemplate("Function"); template = TemplateUtils.ToDeclarationWithModifiersString(m, template); template = TemplateUtils.ReplaceTemplateVariable(template, "Body", selText); template = TemplateUtils.ReplaceTemplateVariable(template, "BlankLine", NewLine); InsertCode(position, template); }
public void GotoPosAndFocus(ScintillaNet.ScintillaControl sci, int position) { int pos = sci.MBSafePosition(position); int line = sci.LineFromPosition(pos); sci.EnsureVisible(line); sci.GotoPos(pos); }
public static void GenerateDelegateMethods(ScintillaNet.ScintillaControl Sci, MemberModel member, Dictionary<MemberModel, ClassModel> selectedMembers, ClassModel classModel, ClassModel inClass) { Sci.BeginUndoAction(); try { string result = TemplateUtils.ReplaceTemplateVariable( TemplateUtils.GetTemplate("DelegateMethodsHeader"), "Class", classModel.Type); int position = -1; ClassModel type; List<string> importsList = new List<string>(); bool isStaticMember = false; if ((member.Flags & FlagType.Static) > 0) isStaticMember = true; inClass.ResolveExtends(); Dictionary<MemberModel, ClassModel>.KeyCollection selectedMemberKeys = selectedMembers.Keys; foreach (MemberModel m in selectedMemberKeys) { MemberModel mCopy = (MemberModel) m.Clone(); string methodTemplate = NewLine; bool overrideFound = false; ClassModel baseClassType = inClass; while (baseClassType != null && !baseClassType.IsVoid()) { MemberList inClassMembers = baseClassType.Members; foreach (MemberModel inClassMember in inClassMembers) { if ((inClassMember.Flags & FlagType.Function) > 0 && m.Name.Equals(inClassMember.Name)) { mCopy.Flags |= FlagType.Override; overrideFound = true; break; } } if (overrideFound) break; baseClassType = baseClassType.Extends; } if (isStaticMember && (m.Flags & FlagType.Static) == 0) mCopy.Flags |= FlagType.Static; if ((m.Flags & FlagType.Setter) > 0) { methodTemplate += TemplateUtils.GetTemplate("Setter"); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Modifiers", (TemplateUtils.GetStaticExternOverride(m) + TemplateUtils.GetModifiers(m)).Trim()); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Name", m.Name); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "EntryPoint", ""); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Type", m.Parameters[0].Type); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Member", member.Name + "." + m.Name); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Void", ASContext.Context.Features.voidKey ?? "void"); } else if ((m.Flags & FlagType.Getter) > 0) { methodTemplate += TemplateUtils.GetTemplate("Getter"); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Modifiers", (TemplateUtils.GetStaticExternOverride(m) + TemplateUtils.GetModifiers(m)).Trim()); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Name", m.Name); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "EntryPoint", ""); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Type", FormatType(m.Type)); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Member", member.Name + "." + m.Name); } else { methodTemplate += TemplateUtils.GetTemplate("Function"); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Body", "<<$(Return) >>$(Body)"); methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "EntryPoint", null); methodTemplate = TemplateUtils.ToDeclarationWithModifiersString(mCopy, methodTemplate); if (m.Type != null && m.Type.ToLower() != "void") methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Return", "return"); else methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Return", null); // check for varargs bool isVararg = false; if (m.Parameters != null && m.Parameters.Count > 0) { MemberModel mm = m.Parameters[m.Parameters.Count - 1]; if (mm.Name.StartsWith("...")) isVararg = true; } string callMethodTemplate = TemplateUtils.GetTemplate("CallFunction"); if (!isVararg) { callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Name", member.Name + "." + m.Name); callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Arguments", TemplateUtils.CallParametersString(m)); callMethodTemplate += ";"; } else { List<MemberModel> pseudoParamsList = new List<MemberModel>(); pseudoParamsList.Add(new MemberModel("null", null, FlagType.ParameterVar, 0)); pseudoParamsList.Add(new MemberModel("[$(Subarguments)].concat($(Lastsubargument))", null, FlagType.ParameterVar, 0)); MemberModel pseudoParamsOwner = new MemberModel(); pseudoParamsOwner.Parameters = pseudoParamsList; callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Name", member.Name + "." + m.Name + ".apply"); callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Arguments", TemplateUtils.CallParametersString(pseudoParamsOwner)); callMethodTemplate += ";"; List<MemberModel> arrayParamsList = new List<MemberModel>(); for (int i = 0; i < m.Parameters.Count - 1; i++) { MemberModel param = m.Parameters[i]; arrayParamsList.Add(param); } pseudoParamsOwner.Parameters = arrayParamsList; callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Subarguments", TemplateUtils.CallParametersString(pseudoParamsOwner)); callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Lastsubargument", m.Parameters[m.Parameters.Count - 1].Name.TrimStart(new char[] { '.', ' '})); } methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Body", callMethodTemplate); } methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "BlankLine", NewLine); result += methodTemplate; if (m.Parameters != null) { for (int i = 0; i < m.Parameters.Count; i++) { MemberModel param = m.Parameters[i]; if (param.Type != null) { type = ASContext.Context.ResolveType(param.Type, selectedMembers[m].InFile); importsList.Add(type.QualifiedName); } } } if (position < 0) { MemberModel latest = GetLatestMemberForFunction(inClass, mCopy.Access, mCopy); if (latest == null) { position = Sci.WordStartPosition(Sci.CurrentPos, true); Sci.SetSel(position, Sci.WordEndPosition(position, true)); } else { position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1); Sci.SetSel(position, position); } } else { position = Sci.CurrentPos; } if (m.Type != null) { type = ASContext.Context.ResolveType(m.Type, selectedMembers[m].InFile); importsList.Add(type.QualifiedName); } } if (importsList.Count > 0 && position > -1) { int o = AddImportsByName(importsList, Sci.LineFromPosition(position)); position += o; Sci.SetSel(position, position); } InsertCode(position, result); } finally { Sci.EndUndoAction(); } }
/// <summary> /// Modified Handler /// </summary> public void SciControl_Modified(ScintillaNet.ScintillaControl sender, int position, int modificationType, string text, int length, int linesAdded, int line, int intfoldLevelNow, int foldLevelPrev) { int l = sender.LineFromPosition(position); if(lastPoint.fileName==sender.FileName&&lastPoint.line==l) { lastPoint.line = sender.LineFromPosition(position); lastPoint.position = position - sender.PositionFromLine(lastPoint.line); return; } MyPoint p = new MyPoint(sender.FileName, position - sender.PositionFromLine(l), l); _list.Push(p); lastPoint = p; Console.WriteLine(sender.FileName + " " + lastPoint.line.ToString() + " " + lastPoint.position.ToString()+" "+_list.Count.ToString()); }
/// <summary> /// Checks if a given search match actually points to the given target source /// </summary /// <returns>True if the SearchMatch does point to the target source.</returns> public static ASResult DeclarationLookupResult(ScintillaNet.ScintillaControl Sci, int position) { if (!ASContext.Context.IsFileValid || (Sci == null)) return null; // get type at cursor position ASResult result = ASComplete.GetExpressionType(Sci, position); // browse to package folder if (result.IsPackage && result.InFile != null) { return null; } // open source and show declaration if (!result.IsNull()) { if (result.Member != null && (result.Member.Flags & FlagType.AutomaticVar) > 0) return null; FileModel model = result.InFile ?? ((result.Member != null && result.Member.InFile != null) ? result.Member.InFile : null) ?? ((result.Type != null) ? result.Type.InFile : null); if (model == null || model.FileName == "") return null; ClassModel inClass = result.InClass ?? result.Type; // for Back command int lookupLine = Sci.LineFromPosition(Sci.CurrentPos); int lookupCol = Sci.CurrentPos - Sci.PositionFromLine(lookupLine); ASContext.Panel.SetLastLookupPosition(ASContext.Context.CurrentFile, lookupLine, lookupCol); // open the file if (model != ASContext.Context.CurrentModel) { // cached files declarations have no line numbers if (model.CachedModel && model.Context != null) { ASFileParser.ParseFile(model); if (inClass != null && !inClass.IsVoid()) { inClass = model.GetClassByName(inClass.Name); if (result.Member != null) result.Member = inClass.Members.Search(result.Member.Name, 0, 0); } else result.Member = model.Members.Search(result.Member.Name, 0, 0); } if (model.FileName.Length > 0 && File.Exists(model.FileName)) { ASContext.MainForm.OpenEditableDocument(model.FileName, false); } else { ASComplete.OpenVirtualFile(model); result.InFile = ASContext.Context.CurrentModel; if (result.InFile == null) return null; if (inClass != null) { inClass = result.InFile.GetClassByName(inClass.Name); if (result.Member != null) result.Member = inClass.Members.Search(result.Member.Name, 0, 0); } else if (result.Member != null) { result.Member = result.InFile.Members.Search(result.Member.Name, 0, 0); } } } if ((inClass == null || inClass.IsVoid()) && result.Member == null) return null; Sci = ASContext.CurSciControl; if (Sci == null) return null; int line = 0; string name = null; bool isClass = false; // member if (result.Member != null && result.Member.LineFrom > 0) { line = result.Member.LineFrom; name = result.Member.Name; } // class declaration else if (inClass.LineFrom > 0) { line = inClass.LineFrom; name = inClass.Name; isClass = true; // constructor foreach (MemberModel member in inClass.Members) { if ((member.Flags & FlagType.Constructor) > 0) { line = member.LineFrom; name = member.Name; isClass = false; break; } } } if (line > 0) // select { if (isClass) ASComplete.LocateMember("(class|interface)", name, line); else ASComplete.LocateMember("(function|var|const|get|set|property|[,(])", name, line); } return result; } return null; }
static private bool HandleDocTagCompletion(ScintillaNet.ScintillaControl Sci) { if (ASContext.CommonSettings.JavadocTags == null || ASContext.CommonSettings.JavadocTags.Length == 0) return false; string txt = Sci.GetLine(Sci.LineFromPosition(Sci.CurrentPos)).TrimStart(); if (!Regex.IsMatch(txt, "^\\*[\\s]*\\@")) return false; // build tag list if (docVariables == null) { docVariables = new List<ICompletionListItem>(); TagItem item; foreach (string tag in ASContext.CommonSettings.JavadocTags) { item = new TagItem(tag); docVariables.Add(item); } } // show CompletionList.Show(docVariables, true, ""); return true; }