예제 #1
0
        /// <summary>
        /// Include files
        /// </summary>
        /// <param name="pars"></param>
        public void Visit(ParsedIncludeFile pars)
        {
            // try to find the file in the propath
            var fullFilePath = ProEnvironment.Current.FindFirstFileInPropath(pars.Name);

            // To code explorer
            _parsedExplorerItemsList.Add(new CodeExplorerItem {
                DisplayText   = pars.Name,
                Branch        = CodeExplorerBranch.Include,
                IsNotBlock    = true,
                Flag          = AddExternalFlag(string.IsNullOrEmpty(fullFilePath) ? CodeExplorerFlag.NotFound : 0),
                DocumentOwner = pars.FilePath,
                GoToLine      = pars.Line,
                GoToColumn    = pars.Column,
                SubString     = SetExternalInclude(null)
            });

            // Parse the include file ?
            if (string.IsNullOrEmpty(fullFilePath))
            {
                return;
            }

            // ensure to not parse the same file twice in a parser session!
            if (_parsedFiles.Contains(fullFilePath))
            {
                return;
            }
            _parsedFiles.Add(fullFilePath);

            ParserVisitor parserVisitor  = ParseFile(fullFilePath, pars.Scope);
            var           parserItemList = parserVisitor._parsedCompletionItemsList.ToList();

            // correct the line number of each parsed element, so we can filter the items correctly in the completion list
            parserItemList.ForEach(data => { if (data.FromParser)
                                             {
                                                 data.ParsedItem.IncludeLine = pars.Line;
                                             }
                                   });

            // add info from the parser
            _parsedCompletionItemsList.AddRange(parserItemList);
            if (Config.Instance.CodeExplorerDisplayExternalItems)
            {
                _parsedExplorerItemsList.AddRange(parserVisitor._parsedExplorerItemsList.ToList());
            }

            // fill the defined procedures dictionnary
            foreach (var definedProcedure in parserVisitor._definedProcedures.Where(definedProcedure => !_definedProcedures.Contains(definedProcedure)))
            {
                _definedProcedures.Add(definedProcedure);
            }
        }
예제 #2
0
        private static void DoParse()
        {
            try {
                if (OnParseStarted != null)
                {
                    OnParseStarted();
                }

                if (_parserLock.TryEnterWriteLock(200))
                {
                    try {
                        // make sure to always parse the current file
                        do
                        {
                            //var watch = Stopwatch.StartNew();

                            _lastParsedFilePath = Plug.CurrentFilePath;

                            // Parse the document
                            _ablParser = new Parser(Plug.IsCurrentFileProgress ? Npp.Text : string.Empty, _lastParsedFilePath, null, true);

                            // visitor
                            _parserVisitor = new ParserVisitor(true, _lastParsedFilePath, _ablParser.LineInfo);
                            _ablParser.Accept(_parserVisitor);

                            //watch.Stop();
                            //UserCommunication.Notify("Updated in " + watch.ElapsedMilliseconds + " ms", 1);
                        } while (!_lastParsedFilePath.Equals(Plug.CurrentFilePath));
                    } finally {
                        _parserLock.ExitWriteLock();
                    }
                }

                if (OnParseEnded != null)
                {
                    OnParseEnded();
                }
            } catch (Exception e) {
                ErrorHandler.ShowErrors(e, "Error in ParseCurrentDocumentTick");
            } finally {
                _parsing = false;
                if (_parseRequestedWhenBusy)
                {
                    ParseCurrentDocumentTick();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Parses a file.
        /// Remarks : it doesn't parse the document against known words since this is only useful for
        /// the CURRENT document and not for the others
        /// </summary>
        private static ParserVisitor ParseFile(string filePath, ParsedScopeItem scopeItem)
        {
            ParserVisitor parserVisitor;

            // did we already parsed this file in a previous parse session? (if we are in CodeExplorerDisplayExternalItems mode we need to parse it again anyway)
            if (SavedPersistent.ContainsKey(filePath))
            {
                parserVisitor = SavedPersistent[filePath];
            }
            else
            {
                // Parse it
                var ablParser = new Parser(Utils.ReadAllText(filePath), filePath, scopeItem, false);
                parserVisitor = new ParserVisitor(false);
                ablParser.Accept(parserVisitor);
            }

            return(parserVisitor);
        }
예제 #4
0
        /// <summary>
        /// Parses a file.
        /// Remarks : it doesn't parse the document against known words since this is only useful for
        /// the CURRENT document and not for the others
        /// </summary>
        private static ParserVisitor ParseFile(string fileName, ParsedScopeItem scopeItem)
        {
            ParserVisitor parserVisitor;

            // did we already parsed this file in a previous parse session?
            if (_savedParserVisitors.ContainsKey(fileName))
            {
                parserVisitor = _savedParserVisitors[fileName];
            }
            else
            {
                // Parse it
                var ablParser = new Parser(Utils.ReadAllText(fileName), fileName, scopeItem);

                parserVisitor = new ParserVisitor(false, Path.GetFileName(fileName), ablParser.LineInfo);
                ablParser.Accept(parserVisitor);

                // save it for future uses
                _savedParserVisitors.Add(fileName, parserVisitor);
            }

            return(parserVisitor);
        }
예제 #5
0
        /// <summary>
        /// Parses given file and load its function + procedures has persistent so they are
        /// accessible from the autocompletion list
        /// Set runPersistentIsInFile = false (default) to add items only to the completion list,
        /// set to true to also display proc/func in the code explorer tree if asked
        /// </summary>
        private void LoadProcPersistent(string fileName, ParsedScopeItem scopeItem)
        {
            ParserVisitor parserVisitor = ParseFile(fileName, scopeItem);

            // add info to the completion list
            var listToAdd = parserVisitor.ParsedCompletionItemsList.Where(data => data is FunctionCompletionItem || data is ProcedureCompletionItem).ToList();

            foreach (var completionData in listToAdd)
            {
                completionData.Flags = completionData.Flags | ParseFlag.Persistent;
            }
            _parsedCompletionItemsList.AddRange(listToAdd);

            // add info to the code explorer
            if (Config.Instance.CodeExplorerDisplayPersistentItems)
            {
                foreach (var codeExplorerItem in parserVisitor.ParsedExplorerItemsList.SelectMany(item => item.Children ?? new List <FilteredTypeTreeListItem>()).Cast <CodeItem>().Where(item => item is FunctionCodeItem || item is ProcedureCodeItem))
                {
                    codeExplorerItem.Flags = codeExplorerItem.Flags | ParseFlag.Persistent;
                    PushToCodeExplorer(codeExplorerItem is FunctionCodeItem ? GetExplorerListNode("Functions", CodeExplorerIconType.Function) : GetExplorerListNode("Procedures", CodeExplorerIconType.Procedure), codeExplorerItem);
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Parses given file and load its function + procedures has persistent so they are
        /// accessible from the autocompletion list
        /// Set runPersistentIsInFile = false (default) to add items only to the completion list,
        /// set to true to also display proc/func in the code explorer tree if asked
        /// </summary>
        public void LoadProcPersistent(string fileName, ParsedScopeItem scopeItem)
        {
            ParserVisitor parserVisitor = ParseFile(fileName, scopeItem);

            // add info to the completion list
            var listToAdd = parserVisitor._parsedCompletionItemsList.Where(data => (data.Type == CompletionType.Function || data.Type == CompletionType.Procedure)).ToList();

            foreach (var completionData in listToAdd)
            {
                completionData.Flag = completionData.Flag | ParseFlag.Persistent;
            }
            _parsedCompletionItemsList.AddRange(listToAdd);

            // add info to the code explorer
            if (Config.Instance.CodeExplorerDisplayExternalItems)
            {
                var listExpToAdd = parserVisitor._parsedExplorerItemsList.Where(item => item.Branch == CodeExplorerBranch.Procedure || item.Branch == CodeExplorerBranch.Function).ToList();
                foreach (var codeExplorerItem in listExpToAdd)
                {
                    codeExplorerItem.Flag = codeExplorerItem.Flag | CodeExplorerFlag.Persistent;
                }
                _parsedExplorerItemsList.AddRange(listExpToAdd);
            }
        }
예제 #7
0
        private static void DoParse()
        {
            try {
                if (OnStart != null)
                {
                    OnStart();
                }

                DoInLock(() => {
                    // make sure to always parse the current file
                    Parser parser = null;
                    do
                    {
                        _lastFilePathParsed = Npp.CurrentFile.Path;

                        if (Npp.CurrentFile.IsProgress)
                        {
                            parser = new Parser(Sci.Text, _lastFilePathParsed, null, true);

                            // visitor
                            var visitor = new ParserVisitor(true);
                            parser.Accept(visitor);

                            // send completionItems
                            if (OnEndSendCompletionItems != null)
                            {
                                OnEndSendCompletionItems(visitor.ParsedCompletionItemsList);
                            }

                            // send codeExplorerItems
                            if (OnEndSendCodeExplorerItems != null)
                            {
                                OnEndSendCodeExplorerItems(visitor.ParsedExplorerItemsList);
                            }
                        }
                        else
                        {
                            var textLexer   = new TextLexer(Sci.GetTextAroundFirstVisibleLine(Config.Instance.NppAutoCompleteMaxLengthToParse), AutoCompletion.CurrentLangAdditionalChars);
                            var textVisitor = new TextLexerVisitor(_lastFilePathParsed)
                            {
                                IgnoreNumbers         = Config.Instance.NppAutoCompleteIgnoreNumbers,
                                MinWordLengthRequired = Config.Instance.NppAutoCompleteMinWordLengthRequired,
                                KnownWords            = KnownWords != null ? new HashSet <string>(KnownWords, AutoCompletion.ParserStringComparer) : new HashSet <string>(AutoCompletion.ParserStringComparer)
                            };
                            textLexer.Accept(textVisitor);

                            // send completionItems
                            if (OnEndSendCompletionItems != null)
                            {
                                OnEndSendCompletionItems(textVisitor.ParsedCompletionItemsList);
                            }

                            // send codeExplorerItems
                            if (OnEndSendCodeExplorerItems != null)
                            {
                                OnEndSendCodeExplorerItems(null);
                            }
                        }
                    } while (!_lastFilePathParsed.Equals(Npp.CurrentFile.Path));

                    if (parser != null)
                    {
                        _lineInfo = new Dictionary <int, LineInfo>(parser.LineInfo);
                    }

                    // send parserItems
                    if (OnEndSendParserItems != null)
                    {
                        if (parser != null)
                        {
                            OnEndSendParserItems(parser.ParserErrors, parser.LineInfo, parser.ParsedItemsList);
                        }
                        else
                        {
                            OnEndSendParserItems(null, null, null);
                        }
                    }
                });

                if (OnEnd != null)
                {
                    OnEnd();
                }
            } catch (Exception e) {
                ErrorHandler.ShowErrors(e, "Error while analyzing the current document");
            }
        }