Пример #1
0
        /// <summary>
        /// Renames unit names found after the a keyword and before semicolen (;)
        /// </summary>
        /// <param name="aKeywordToken"></param>
        /// <param name="aOldUnitName"></param>
        /// <param name="aNewUnitName"></param>
        /// <param name="aVsTextLines"></param>
        /// <returns></returns>
        protected virtual bool RenameUnitAfterKeyword(DelphiToken aKeywordToken, string aOldUnitName, string aNewUnitName, IVsTextLines aVsTextLines)
        {
            TUnitScanner lScanner = new TUnitScanner(aVsTextLines);
            bool         lResult  = false;

            // any data found before Program,Package,Library,Unit is not valid
            lScanner.EndToken = DelphiToken.None;
            while (lScanner.GotoNextToken(aKeywordToken))
            {
                lScanner.EndToken = DelphiToken.EndStatement;
                while (lScanner.GotoNextToken(DelphiToken.Identifier))
                {
                    if (String.Compare(lScanner.TokenValue, aOldUnitName, true) == 0)
                    {
                        ReplaceText(lScanner, aNewUnitName);
                        AfterRenameUnitName(lScanner.TokenValue, aNewUnitName, lScanner); // for project files
                        lResult = true;
                        //
                        // NOTE: We keep looking just in case there are compiler directives.
                        //
                        lScanner.EndToken = DelphiToken.EndStatement;
                    }
                }
                lScanner.EndToken = DelphiToken.None;
                //
                // NOTE: Look for all uses clases just in case the are in compiler directives.
                //
            }
            return(lResult);
        }
Пример #2
0
        protected virtual string[] GetUnitFilesAfterKeyword(DelphiToken aKeyword)
        {
            List <string> lResult = new List <string>();

            try
            {
                TUnitScanner lScanner = new TUnitScanner(GetTextLines());
                lScanner.EndToken = DelphiToken.None;
                if (lScanner.GotoNextToken(aKeyword))
                {
                    lScanner.EndToken = DelphiToken.EndStatement;
                    while (lScanner.GotoNextToken(DelphiToken.String))
                    {
                        lResult.Add(TDelphiSource.DelphiStringToText(lScanner.TokenValue));
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessageBox(ex.Message, "GetUnitFilesAfterKeyword() Error");
            }
            return(lResult.ToArray());
        }
        public bool GotoNextToken(DelphiToken aToken, out int aStartLine, out int aStartIndex, out int aEndLine, out int aEndIndex)
        {
            int       lCurrLine, lCurrIndex, lLineLen, lState = 0;
            int       lLastState = 0;
            string    lLineText  = "";
            TokenInfo lTokenInfo = new TokenInfo();

            lCurrLine  = StartLine;
            lCurrIndex = StartIndex;
            int lAddChrs = StartIndex;

            aStartIndex = 0;
            aStartLine  = 0;
            aEndIndex   = 0;
            aEndLine    = 0;
            lLineLen    = 0;
            if (aToken == DelphiToken.None)
            {
                return(false);
            }
            System.Diagnostics.Debug.Assert(aToken != EndToken, "End Token and aToken parameter match in GotoNextToken");
            bool result = false;

            while (lCurrLine <= EndLine)
            {
                if (lCurrLine == EndLine - 2)
                {
                    result = false;
                }
                if (lLineText == "")
                {
                    // TODO: Read and check out whats the diff between NativeMethods.ThrowOnFailure and ErrorHandler.ThrowOnFailure?
                    ErrorHandler.ThrowOnFailure(FTextLines.GetLengthOfLine(lCurrLine, out lLineLen));
                    ErrorHandler.ThrowOnFailure(FTextLines.GetLineText(lCurrLine, 0, lCurrLine, lLineLen, out lLineText));
                    FScanner.SetSource(lLineText, lCurrIndex);
                }
                lTokenInfo.Type     = TokenType.Unknown;
                lTokenInfo.EndIndex = lLineLen - 1;
                try
                {
                    FScanner.ScanTokenAndProvideInfoAboutIt(lTokenInfo, ref lState);
                }
                catch { /* Do we care? */ }

                // lTokenInfo.StartIndex will = 0 for the first token even if
                // in line "FScanner.SetSource(lLineText, lCurrIndex)" lCurrIndex > 0
                // so we will need to add some values to make the values correct.
                if (lAddChrs > 0)
                {
                    lTokenInfo.EndIndex   += lAddChrs;
                    lTokenInfo.StartIndex += lAddChrs;
                }

                // if we have reached your end token then exit
                if (lTokenInfo.Token == (int)EndToken && lState == DelphiScanner.YYINITIAL)
                {
                    break;
                }
                aEndIndex  = lTokenInfo.EndIndex;   // set end point
                aEndLine   = lCurrLine;             // set end line
                lCurrIndex = lTokenInfo.StartIndex; // Set new point

                if ((lState == DelphiScanner.YYINITIAL) && (lTokenInfo.Token == (int)aToken) && (lState != lLastState))
                {
                    // no worries we found have the StartIndex and StartLine already
                    result = true;
                }
                else if ((lState == DelphiScanner.YYINITIAL) || (lState != lLastState))
                {
                    aStartLine  = lCurrLine;
                    aStartIndex = lCurrIndex;
                    lLastState  = lState;
                    result      = (lTokenInfo.Token == (int)aToken);
                }

                if (result)
                {
                    break;
                }
                lCurrIndex = lTokenInfo.EndIndex + 1;
                if (lCurrIndex >= lLineLen)
                {
                    lLineText  = "";
                    lCurrIndex = 0;
                    lAddChrs   = 0;
                    lCurrLine++;
                }
            }
            if (result)
            {
                StartIndex = lTokenInfo.EndIndex + 1;
                StartLine  = aEndLine;
                // aEndIndex is correct but the GetLineText method does not read the text out correctly.
                ErrorHandler.ThrowOnFailure(FTextLines.GetLineText(aStartLine, aStartIndex, aEndLine, aEndIndex + 1, out FTokenValue));
            }
            return(result);
        }
        public bool GotoNextToken(DelphiToken aToken)
        {
            int lStartLine, lEndLine, lStartIndex, lEndIndex;

            return(GotoNextToken(aToken, out lStartLine, out lStartIndex, out lEndLine, out lEndIndex));
        }