private void RewriteCall(VBAParser.ArgsCallContext paramList, CodeModule module) { var paramNames = paramList.argCall().Select(arg => arg.GetText()).ToList(); var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount].Replace(" _" + Environment.NewLine, string.Empty).RemoveExtraSpacesLeavingIndentation(); var parameterIndex = 0; var currentStringIndex = 0; for (var i = 0; i < paramNames.Count && parameterIndex < _model.Parameters.Count; i++) { var parameterStringIndex = newContent.IndexOf(paramNames.ElementAt(i), currentStringIndex, StringComparison.Ordinal); if (parameterStringIndex <= -1) { continue; } var oldParameterString = paramNames.ElementAt(i); var newParameterString = paramNames.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index); var beginningSub = newContent.Substring(0, parameterStringIndex); var replaceSub = newContent.Substring(parameterStringIndex).Replace(oldParameterString, newParameterString); newContent = beginningSub + replaceSub; parameterIndex++; currentStringIndex = beginningSub.Length + newParameterString.Length; } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void AddParameter(Declaration targetMethod, Declaration targetVariable, VBAParser.ArgListContext paramList, CodeModule module) { var argList = paramList.arg(); var lastParam = argList.LastOrDefault(); var newContent = GetOldSignature(targetMethod); if (lastParam == null) { // Increase index by one because VBA is dumb enough to use 1-based indexing newContent = newContent.Insert(newContent.IndexOf('(') + 1, GetParameterDefinition(targetVariable)); } else if (targetMethod.DeclarationType != DeclarationType.PropertyLet && targetMethod.DeclarationType != DeclarationType.PropertySet) { newContent = newContent.Replace(argList.Last().GetText(), argList.Last().GetText() + ", " + GetParameterDefinition(targetVariable)); } else { newContent = newContent.Replace(argList.Last().GetText(), GetParameterDefinition(targetVariable) + ", " + argList.Last().GetText()); } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, paramList.GetSelection().LineCount - 1); }
private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, CodeModule module) { var argList = paramList.arg(); var newContent = GetOldSignature(target); var lineNum = paramList.GetSelection().LineCount; var parameterIndex = 0; var currentStringIndex = 0; for (var i = parameterIndex; i < _model.Parameters.Count; i++) { var oldParam = argList.ElementAt(parameterIndex).GetText(); var newParam = argList.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index).GetText(); var parameterStringIndex = newContent.IndexOf(oldParam, currentStringIndex, StringComparison.Ordinal); if (parameterStringIndex > -1) { var beginningSub = newContent.Substring(0, parameterStringIndex); var replaceSub = newContent.Substring(parameterStringIndex).Replace(oldParam, newParam); newContent = beginningSub + replaceSub; parameterIndex++; currentStringIndex = beginningSub.Length + newParam.Length; } } module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineNum - 1); }
private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, CodeModule module) { var parameters = paramList.arg().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList(); var reorderedParams = new List <string>(); foreach (var param in _model.Parameters) { var parameterAtIndex = parameters.SingleOrDefault(s => s.Index == param.Index); if (parameterAtIndex != null) { reorderedParams.Add(parameterAtIndex.Text); } } // property let/set and paramarrays for (var index = reorderedParams.Count; index < parameters.Count; index++) { reorderedParams.Add(parameters[index].Text); } var signature = GetOldSignature(target); signature = signature.Remove(signature.IndexOf('(')); var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText(); signature += '(' + string.Join(", ", reorderedParams) + ")" + (asTypeText == " " ? string.Empty : asTypeText); var lineCount = paramList.GetSelection().LineCount; module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void RewriteCall(VBAParser.ArgumentListContext paramList, CodeModule module) { List <string> paramNames = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } else { return(string.Empty); } }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { paramNames.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount].Replace(" _" + Environment.NewLine, string.Empty).RemoveExtraSpacesLeavingIndentation(); var parameterIndex = 0; var currentStringIndex = 0; for (var i = 0; i < paramNames.Count && parameterIndex < _model.Parameters.Count; i++) { var parameterStringIndex = newContent.IndexOf(paramNames.ElementAt(i), currentStringIndex, StringComparison.Ordinal); if (parameterStringIndex <= -1) { continue; } var oldParameterString = paramNames.ElementAt(i); var newParameterString = paramNames.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index); var beginningSub = newContent.Substring(0, parameterStringIndex); var replaceSub = newContent.Substring(parameterStringIndex).Replace(oldParameterString, newParameterString); newContent = beginningSub + replaceSub; parameterIndex++; currentStringIndex = beginningSub.Length + newParameterString.Length; } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void RemoveCallParameter(VBAParser.ArgsCallContext paramList, CodeModule module) { var paramNames = paramList.argCall().Select(arg => arg.GetText()).ToList(); var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount].Replace(" _" + Environment.NewLine, string.Empty).RemoveExtraSpacesLeavingIndentation(); var currentStringIndex = 0; foreach ( var param in _model.Parameters.Where(item => item.IsRemoved && item.Index < paramNames.Count) .Select(item => item.Declaration)) { var paramIndex = _model.Parameters.FindIndex(item => item.Declaration.Context.GetText() == param.Context.GetText()); if (paramIndex >= paramNames.Count) { return; } do { var paramToRemoveName = paramNames.ElementAt(0).Contains(":=") ? paramNames.Find(item => item.Contains(param.IdentifierName + ":=")) : paramNames.ElementAt(paramIndex); if (paramToRemoveName == null || !newContent.Contains(paramToRemoveName)) { continue; } var valueToRemove = paramToRemoveName != paramNames.Last() ? paramToRemoveName + "," : paramToRemoveName; var parameterStringIndex = newContent.IndexOf(valueToRemove, currentStringIndex, StringComparison.Ordinal); if (parameterStringIndex <= -1) { continue; } newContent = newContent.Remove(parameterStringIndex, valueToRemove.Length); currentStringIndex = parameterStringIndex; if (paramToRemoveName == paramNames.Last() && newContent.LastIndexOf(',') != -1) { newContent = newContent.Remove(newContent.LastIndexOf(','), 1); } } while (paramIndex >= _model.Parameters.Count - 1 && ++paramIndex < paramNames.Count && newContent.Contains(paramNames.ElementAt(paramIndex))); } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
public IntPtr WindowProc(IntPtr hwnd, uint msg, IntPtr WParam, IntPtr LParam) { // WM_CHAR if (msg == 0x102) { int chr = WParam.ToInt32(); if (chr == '\x9') { //MessageBox.Show("Tab", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); int x, y, w, h; _vbe.ActiveCodePane.GetSelection(out x, out y, out h, out w); Debug.WriteLine(String.Format("x : {0}, y : {1}, h : {2}, w : {3}", x, y, h, w)); CodeModule cm = _vbe.ActiveCodePane.CodeModule; string line = cm.Lines[x, 1]; Debug.WriteLine("the code :" + line); line = line.TrimEnd(); string token = findTokenReverse(line); if (token == "if") { // paste if stat line = line.Substring(0, line.Length - 2); String[] strs = new String[3]; var l = line.Length; strs[0] = line + "If Then"; strs[1] = ""; strs[2] = new String(' ', line.Length) + "End If"; cm.DeleteLines(x); cm.InsertLines(x, String.Join("\r\n", strs)); // set pos _vbe.ActiveCodePane.SetSelection(x, y, h, w); } } Debug.WriteLine("the chr : " + Convert.ToChar(chr)); } if (msg == 0x14) { Debug.WriteLine("EraseBackgroud"); } if (msg == 0x85) { Debug.WriteLine("NC PAINT"); } return(NativeMethods.CallWindowProc(oldProc, hwnd, msg, WParam, LParam)); }
public override void Fix() { dynamic functionContext = Context as VBAParser.FunctionStmtContext; dynamic propertyGetContext = Context as VBAParser.PropertyGetStmtContext; var context = functionContext ?? propertyGetContext; if (context == null) { throw new InvalidOperationException(string.Format(InspectionsUI.InvalidContextTypeInspectionFix, Context.GetType(), GetType())); } VBAParser.FunctionNameContext functionName = null; if (Context is VBAParser.FunctionStmtContext) { functionName = ((VBAParser.FunctionStmtContext)Context).functionName(); } else { functionName = ((VBAParser.PropertyGetStmtContext)Context).functionName(); } string token = functionContext != null ? Tokens.Function : Tokens.Property + ' ' + Tokens.Get; string endToken = token == Tokens.Function ? token : Tokens.Property; string visibility = context.visibility() == null ? string.Empty : context.visibility().GetText() + ' '; string name = ' ' + Identifier.GetName(functionName.identifier()); bool hasTypeHint = Identifier.GetTypeHintValue(functionName.identifier()) != null; string args = context.argList().GetText(); string asType = context.asTypeClause() == null ? string.Empty : ' ' + context.asTypeClause().GetText(); string oldSignature = visibility + token + name + (hasTypeHint ? Identifier.GetTypeHintValue(functionName.identifier()) : string.Empty) + args + asType; string newSignature = visibility + Tokens.Sub + name + args; string procedure = Context.GetText(); string noReturnStatements = procedure; _returnStatements.ToList().ForEach(returnStatement => noReturnStatements = Regex.Replace(noReturnStatements, @"[ \t\f]*" + returnStatement + @"[ \t\f]*\r?\n?", "")); string result = noReturnStatements.Replace(oldSignature, newSignature) .Replace(Tokens.End + ' ' + endToken, Tokens.End + ' ' + Tokens.Sub) .Replace(Tokens.Exit + ' ' + endToken, Tokens.Exit + ' ' + Tokens.Sub); CodeModule module = Selection.QualifiedName.Component.CodeModule; Selection selection = Context.GetSelection(); module.DeleteLines(selection.StartLine, selection.LineCount); module.InsertLines(selection.StartLine, result); }
private void RewriteCall(VBAParser.ArgumentListContext paramList, CodeModule module) { var argValues = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { argValues.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } return(string.Empty); }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { argValues.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { argValues.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount]; newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); var reorderedArgValues = new List <string>(); foreach (var param in _model.Parameters) { var argAtIndex = argValues.ElementAtOrDefault(param.Index); if (argAtIndex != null) { reorderedArgValues.Add(argAtIndex); } } // property let/set and paramarrays for (var index = reorderedArgValues.Count; index < argValues.Count; index++) { reorderedArgValues.Add(argValues[index]); } newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", reorderedArgValues)); module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void RemoveSignatureParameters(Declaration target, VBAParser.ArgListContext paramList, CodeModule module) { // property set/let have one more parameter than is listed in the getter parameters var nonRemovedParamNames = paramList.arg().Where((a, s) => s >= _model.Parameters.Count || !_model.Parameters[s].IsRemoved).Select(s => s.GetText()); var signature = GetOldSignature(target); signature = signature.Remove(signature.IndexOf('(')); var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText(); signature += '(' + string.Join(", ", nonRemovedParamNames) + ")" + (asTypeText == " " ? string.Empty : asTypeText); var lineCount = paramList.GetSelection().LineCount; module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void RemoveSignatureParameters(Declaration target, VBAParser.ArgListContext paramList, CodeModule module) { var paramNames = paramList.arg(); var paramsRemoved = _model.Parameters.Where(item => item.IsRemoved).ToList(); var signature = GetOldSignature(target); foreach (var param in paramsRemoved) { try { signature = ReplaceCommas(signature.Replace(paramNames.ElementAt(param.Index).GetText(), string.Empty), _model.Parameters.FindIndex(item => item == param) - paramsRemoved.FindIndex(item => item == param)); } catch (ArgumentOutOfRangeException) { } } var lineNum = paramList.GetSelection().LineCount; module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineNum - 1); }
private void RemoveCallParameter(VBAParser.ArgumentListContext paramList, CodeModule module) { var paramNames = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } return(string.Empty); }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { paramNames.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount]; newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); var savedParamNames = paramNames; for (var index = _model.Parameters.Count - 1; index >= 0; index--) { var param = _model.Parameters[index]; if (!param.IsRemoved) { continue; } if (param.Name.Contains("ParamArray")) { // handle param arrays while (savedParamNames.Count > index) { savedParamNames.RemoveAt(index); } } else { if (index < savedParamNames.Count && !savedParamNames[index].StripStringLiterals().Contains(":=")) { savedParamNames.RemoveAt(index); } else { var paramIndex = savedParamNames.FindIndex(s => s.StartsWith(param.Declaration.IdentifierName + ":=")); if (paramIndex != -1 && paramIndex < savedParamNames.Count) { savedParamNames.RemoveAt(paramIndex); } } } } newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", savedParamNames)); module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private bool InjectCodeToAccess(string srcDir) { // get project name(s) foreach (VBProject vbp in AccApp.VBE.VBProjects) { var pjtName = vbp.Name; // get project directory var pjtDir = Directory.GetDirectories(srcDir, pjtName); // skip when not found if (pjtDir == null || pjtDir.Length == 0) { continue; } foreach (var srcFile in Directory.GetFiles(pjtDir[0], "*", SearchOption.AllDirectories)) { var fName = Path.GetFileNameWithoutExtension(srcFile); if (fName == null || fName == "") { continue; } string srcContent = null; using (var sr = new StreamReader(srcFile, Encoding.UTF8)) { srcContent = sr.ReadToEnd(); } bool isFound = false; foreach (VBComponent vbc in vbp.VBComponents) { CodeModule module = vbc.CodeModule; if (module == null) { continue; } //UPDATE module when found if (module.Name == fName) { if (vbc.Type == vbext_ComponentType.vbext_ct_Document) { AccApp.DoCmd.OpenForm(FormName: fName.Substring(5) , View: Access.AcFormView.acDesign , WindowMode: Access.AcWindowMode.acHidden); //AccApp.DoCmd.OpenForm(FormName: module.Name // , View: Access.AcFormView.acDesign // , WindowMode: Access.AcWindowMode.acHidden); } else { AccApp.DoCmd.OpenModule(module.Name); } //AccApp.DoCmd.OpenModule(module.Name); module.DeleteLines(1, module.CountOfLines); module.AddFromString(srcContent); if (vbc.Type == vbext_ComponentType.vbext_ct_Document) { AccApp.DoCmd.Save(Access.AcObjectType.acForm, fName.Substring(5)); //AccApp.DoCmd.Save(Access.AcObjectType.acForm, module.Name); } else { AccApp.DoCmd.Save(Access.AcObjectType.acModule, module.Name); } //AccApp.DoCmd.Close(Access.AcObjectType.acModule, module.Name, Access.AcCloseSave.acSaveYes); isFound = true; break; } } // INSERT module when not found if (!isFound) { //Get sub-directories //Document,StdModule,ClassModule var dir = Directory.GetParent(srcFile).Name; //var pjtName = Directory.GetParent(Directory.GetParent(srcFile).FullName).Name; vbext_ComponentType moduleType; if (Regex.IsMatch(dir, "StdModule")) { moduleType = vbext_ComponentType.vbext_ct_StdModule; } else if (Regex.IsMatch(dir, "ClassModule")) { moduleType = vbext_ComponentType.vbext_ct_ClassModule; } else { continue; // DO NOT ADD form module via interop, use MS-ACCESS Export menu instead } foreach (VBProject pjt in AccApp.VBE.VBProjects) { if (pjt.Name == pjtName) { VBComponent module = pjt.VBComponents.Add(moduleType); module.Name = fName; module.CodeModule.DeleteLines(1, module.CodeModule.CountOfLines); module.CodeModule.AddFromString(srcContent); AccApp.DoCmd.Save(Access.AcObjectType.acModule, fName); break; } } } } } return(true); }
/// <summary> /// Deletes all lines from the CodeModule /// </summary> public static void Clear(this CodeModule module) { module.DeleteLines(1, module.CountOfLines); }
public void DeleteLines(int StartLine, int Count = 1) { _codeModule.DeleteLines(StartLine, Count); }
/// <summary> /// Deletes the lines containing the selection. /// </summary> /// <param name="module"></param> /// <param name="selection"></param> public static void DeleteLines(this CodeModule module, Selection selection) { module.DeleteLines(selection.StartLine, selection.LineCount); }