/// <summary> /// Tries to re-indent the code of the whole document /// </summary> public static void CorrectCodeIndentation() { // handle spam (1s min between 2 indent) if (Utils.IsSpamming("CorrectCodeIndentation", 1000)) { return; } var parser = new Parser.Pro.Parse.Parser(Sci.Text, Npp.CurrentFileInfo.Path, null, false); // in case of an incorrect document, warn the user var parserErrors = parser.ParseErrorsInHtml; if (!string.IsNullOrEmpty(parserErrors)) { if (UserCommunication.Message("The internal parser of 3P has found inconsistencies in your document :<br>" + parserErrors + "<br>You can still try to format your code but the result is not guaranteed (worst case scenario you can press CTRL+Z).<br>If the code compiles successfully and the document is incorrectly formatted, please make sure to create an issue on the project's github and (if possible) include the incriminating code so i can fix this problem : <br>" + Config.UrlIssues.ToHtmlLink() + "<br><br>Please confirm that you want to proceed", MessageImg.MsgQuestion, "Correct indentation", "Problems spotted", new List <string> { "Continue", "Abort" }) != 0) { return; } } // start indenting Sci.BeginUndoAction(); var indentStartLine = Sci.LineFromPosition(Sci.SelectionStart); var indentEndLine = Sci.LineFromPosition(Sci.SelectionEnd); var indentWidth = Sci.TabWidth; var i = 0; var dic = GetIndentation(parser.LineInfo); while (dic.ContainsKey(i)) { if (indentStartLine == indentEndLine || (i >= indentStartLine && i <= indentEndLine)) { var line = Sci.GetLine(i); line.Indentation = (dic[i].BlockDepth + dic[i].ExtraStatementDepth) * indentWidth; } i++; } Sci.EndUndoAction(); }
/// <summary> /// Parse the current document /// </summary> private void ParseNow() { _parser = new Parser.Pro.Parse.Parser(Sci.Text, Npp.CurrentFileInfo.Path, null, false); _parsedItems = _parser.ParsedItemsList.Where(item => !item.Flags.HasFlag(ParseFlag.FromInclude)).ToNonNullList(); }
/// <summary> /// Check the validity of a progress code in the point of view of the appbuilder (make sure it can be opened within the appbuilder) /// </summary> public static void DisplayParserErrors(bool silent = false) { if (Npp.CurrentFileInfo.IsProgress && !_displayParserErrorsIgnoredFiles.Contains(Npp.CurrentFileInfo.Path)) { Task.Factory.StartNew(() => { var currentFilePath = Npp.CurrentFileInfo.Path; UserCommunication.CloseUniqueNotif("DisplayParserErrors" + currentFilePath); var message = new StringBuilder(); message.Append("The analyzed file was :<br>" + currentFilePath.ToHtmlLink() + "<br>"); var parser = new Parser.Pro.Parse.Parser(Sci.Text, currentFilePath, null, false); var parserErrors = parser.ParseErrorsInHtml; if (!string.IsNullOrEmpty(parserErrors)) { message.Append("<br>The parser found syntax errors.<br>You should consider solving those issues in the given order :<br>"); message.Append(parserErrors); } var blockTooLong = new StringBuilder(); foreach (var scope in parser.ParsedItemsList.Where(item => item is ParsedImplementation || item is ParsedProcedure || item is ParsedOnStatement).Cast <ParsedScopeBlock>()) { if (CheckForTooMuchChar(scope)) { blockTooLong.AppendLine("<div>"); blockTooLong.AppendLine(" - " + (scope.FilePath + "|" + scope.Line).ToHtmlLink("Line " + (scope.Line + 1) + " : <b>" + scope.Name + "</b>") + " (" + NbExtraCharBetweenLines(scope.Line, scope.EndBlockLine) + " extra chars)"); blockTooLong.AppendLine("</div>"); } } if (blockTooLong.Length > 0) { message.Append("<br>This file is currently unreadable in the AppBuilder.<br>The following blocks contain more characters than the max limit (" + Config.Instance.GlobalMaxNbCharInBlock + " characters) :<br>"); message.Append(blockTooLong); message.Append("<br><i>To prevent this, reduce the number of characters in the above blocks.<br>Deleting dead code and trimming spaces is a good place to start!</i>"); } // no errors var noProb = blockTooLong.Length == 0 && string.IsNullOrEmpty(parserErrors); if (noProb) { if (silent) { return; } message.Append("No problems found!"); } else { if (silent) { message.Append("<br><br>" + "disable".ToHtmlLink("Click here to disable the automatic check for this file")); } } UserCommunication.NotifyUnique("DisplayParserErrors" + currentFilePath, message.ToString(), noProb ? MessageImg.MsgOk : MessageImg.MsgWarning, "Check code validity", "Analysis results", args => { if (args.Link.Equals("disable")) { args.Handled = true; UserCommunication.CloseUniqueNotif("DisplayParserErrors" + currentFilePath); if (!_displayParserErrorsIgnoredFiles.Contains(currentFilePath)) { _displayParserErrorsIgnoredFiles.Add(currentFilePath); } } }, noProb ? 5 : 0); }); } }