Пример #1
0
        private bool DoSearch(string searchString)
        {
            Int32  TmpCurrentCursorPos;
            Int32  TmpNextCursorPos = -1;
            string TmpS;
            bool   RetB = false;

            if (!FTextEditorControl == null)
            {
                TmpCurrentCursorPos = FTextEditorControl.ActiveTextAreaControl.Caret.Offset;
                TmpS = FTextEditorControl.Document.TextContent.ToLower;
                if (TmpS.Length > 0)
                {
                    //search from cursor to end
                    if (TmpS.Length > TmpCurrentCursorPos + 1)
                    {
                        TmpNextCursorPos = TmpS.IndexOf(searchString.ToLower, TmpCurrentCursorPos + 1);
                    }
                    if (TmpNextCursorPos == -1)
                    {
                        //search from beginning
                        TmpNextCursorPos = TmpS.IndexOf(searchString.ToLower);
                    }
                    if (TmpNextCursorPos > -1)
                    {
                        //found, set cursor
                        // ERROR: Not supported in C#: WithStatement

                        //result ok
                        RetB = true;
                    }
                }
            }
            return(RetB);
        }
Пример #2
0
        private string ConvertSource(string csSrc, PreParseResult preResult)
        {
            IRefactoryProvider TmpRefactoryProvider;
            Int32 TmpI;
            Int32 TmpLine;

            string[] TmpLines;
            string   TmpS;
            string   RetS = "";

            //get current RefacotryProvider
            TmpRefactoryProvider = SettingsManager.CurrentRefactoryProvider;
            //convert with the RefactoryProvider
            try {
                RetS = TmpRefactoryProvider.ConvertCStoVB(csSrc);
            } catch (NetVertException ex) {
                //parser error, modify text and re-throw exception
                TmpS     = ex.Message.Substring(8, ex.Message.Length - 9);
                TmpI     = TmpS.IndexOf(" ");
                TmpLine  = Int32.Parse(TmpS.Substring(0, TmpI));
                TmpLine -= preResult.GetShift(TmpLine);
                if (TmpLine == 0)
                {
                    TmpLine = 1;
                }
                TmpS     = "Line " + TmpLine.ToString + TmpS.Substring(TmpI, TmpS.Length - TmpI);
                TmpLines = preResult.OriginalSourceLines;
                if (TmpLine > 1)
                {
                    if (TmpLine > 2)
                    {
                        TmpS += vbCrLf + "==== " + TmpLines(TmpLine - 3);
                    }
                    TmpS += vbCrLf + "==== " + TmpLines(TmpLine - 2);
                }
                if (TmpLines.Length >= TmpLine)
                {
                    TmpS += vbCrLf + "==>> " + TmpLines(TmpLine - 1);
                    if (TmpLines.Length > TmpLine)
                    {
                        TmpS += vbCrLf + "==== " + TmpLines(TmpLine);
                        if (TmpLines.Length > (TmpLine + 1))
                        {
                            TmpS += vbCrLf + "==== " + TmpLines(TmpLine + 1);
                        }
                    }
                }
                throw new NetVertException(TmpS);
            }
            return(RetS);
        }
Пример #3
0
        //SHARED
        #region Convert Functions

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Convert ASP.NET code using language-autodetection.
        /// </summary>
        /// <param name="aspxSource">The ASPX source code.</param>
        /// <returns>New instance of Class ASPXConverter</returns>
        /// <remarks>
        /// Supported is only ASP.NET code with inline serverside code of the languages Visual Basic .NET or C#.
        /// </remarks>
        /// -----------------------------------------------------------------------------
        public static ASPXConverter Convert(string aspxSource, ConverterLanguagesAutodetectable converterLanguage = ConverterLanguagesAutodetectable.Autodetect)
        {
            ASPXConverter RetConv = new ASPXConverter();
            Int32         TmpStart;
            Int32         TmpEnd;
            Int32         TmpLastEnd;
            string        TmpDirective;
            string        TmpComment = "";
            string        TmpCodeLines;
            string        TmpConvertCode = "";
            ArrayList     TmpHtmlBlocks  = new ArrayList();
            string        TmpS;

            try {
                //set language
                if (converterLanguage == ConverterLanguagesAutodetectable.Autodetect)
                {
                    RetConv.FConverterLanguage = SearchLanguage(aspxSource);
                }
                else
                {
                    RetConv.FConverterLanguage = converterLanguage;
                }

                //get Before-Convert-Comment-Sign
                switch (RetConv.FConverterLanguage)
                {
                case ConverterLanguagesAutodetectable.CSharpToVBNet:
                    TmpComment = "//";

                case ConverterLanguagesAutodetectable.VBNetToCSharp:
                    TmpComment = "'";
                }

                //for each <%...%> block
                TmpStart   = -1;
                TmpLastEnd = 0;
                do
                {
                    //search "<%"
                    TmpStart = aspxSource.IndexOf("<%", TmpStart + 1);
                    if (TmpStart > -1)
                    {
                        //search "%>"
                        TmpEnd = aspxSource.IndexOf("%>", TmpStart + 2);
                        if (TmpEnd > -1)
                        {
                            if (aspxSource.Substring(TmpStart + 2, 1) == "@")
                            {
                                //if @: replace language-attribute and codebehind file extension
                                TmpDirective = aspxSource.Substring(TmpStart, TmpEnd - TmpStart + 2);
                                switch (RetConv.FConverterLanguage)
                                {
                                case ConverterLanguagesAutodetectable.CSharpToVBNet:
                                    TmpDirective = TmpDirective.Replace("\"c#\"", "\"vb\"").Replace(".cs\"", ".vb\"").Replace("=cs ", "=vb ").Replace("=c# ", "=vb ").Replace("\"cs\"", "\"vb\"");

                                case ConverterLanguagesAutodetectable.VBNetToCSharp:
                                    TmpDirective = TmpDirective.Replace("\"vb\"", "\"c#\"").Replace(".vb\"", ".cs\"").Replace("=vb ", "=c# ");
                                }
                                aspxSource = aspxSource.Substring(0, TmpStart) + TmpDirective + aspxSource.Substring(TmpEnd + 2, aspxSource.Length - TmpEnd - 2);
                            }
                            else
                            {
                                //else: convert statements
                                TmpHtmlBlocks.Add(aspxSource.Substring(TmpLastEnd, TmpStart - TmpLastEnd + 2));
                                TmpConvertCode += TmpComment + "HTMLBLOCK-" + TmpHtmlBlocks.Count.ToString + "." + vbCrLf;
                                TmpCodeLines    = aspxSource.Substring(TmpStart + 2, TmpEnd - TmpStart - 2);
                                TmpS            = TmpCodeLines.TrimStart(char.Parse(" "), Chr(9), Chr(10), Chr(13));
                                if ((TmpS != "") && (TmpS.Substring(0, 1) == "="))
                                {
                                    //Add Temporary Out-Variable-Prefix
                                    TmpCodeLines = "NETVERTASPNETOUT " + TmpS;
                                }
                                try {
                                } catch (Exception ex) {
                                }
                                //if StrTrimmLeft(StrTrimmLeft(StrTrimmLeft(tmpcodelines," "), chr(9).ToString)
                                TmpConvertCode += TmpCodeLines + vbCrLf;
                                TmpLastEnd      = TmpEnd;
                            }
                        }
                    }
                } while (TmpStart > -1);
                //Append last HTML-Block
                if (aspxSource.Length > TmpLastEnd)
                {
                    TmpHtmlBlocks.Add(aspxSource.Substring(TmpLastEnd, aspxSource.Length - TmpLastEnd));
                    TmpConvertCode += TmpComment + "HTMLBLOCK-" + TmpHtmlBlocks.Count.ToString + "." + vbCrLf;
                }
                //Convert Code
                switch (RetConv.FConverterLanguage)
                {
                case ConverterLanguagesAutodetectable.CSharpToVBNet:
                    // ERROR: Not supported in C#: WithStatement

                    //get After-Convert-Comment-Sign
                    TmpComment = "'";

                case ConverterLanguagesAutodetectable.VBNetToCSharp:
                    // ERROR: Not supported in C#: WithStatement

                    //get After-Convert-Comment-Sign
                    TmpComment = "//";
                }

                //Remove Temporary Out-Variable-Prefix
                TmpConvertCode = TmpConvertCode.Replace("NETVERTASPNETOUT ", "");
                //Insert HTML-Blocks
                for (Int32 iBlock = 0; iBlock <= TmpHtmlBlocks.Count - 1; iBlock++)
                {
                    TmpConvertCode = TmpConvertCode.Replace(TmpComment + "HTMLBLOCK-" + (iBlock + 1).ToString + ".", TmpHtmlBlocks(iBlock));
                }

                //set result
                RetConv.FResultSource = TmpConvertCode;

#if (DEBUG)
                //catch only internal exceptions
            } catch (NetVertException nEx) {
                RetConv.FHasError  = true;
                RetConv.FErrorText = nEx.Message;
#Else
                //catch all exceptions
            } catch (Exception ex) {
                RetConv.FHasError  = true;
                RetConv.FErrorText = ex.Message;
#endif
            }
            return(RetConv);
        }
Пример #4
0
        private bool NextCodeLineHasNoneOfKeywords(string[] codeLines, Int32 currentLine, string[] keywords)
        {
            string TmpS;
            bool   RetB = true;

            TmpS = GetNextCodeLine(codeLines, currentLine);
            for (Int32 I = 0; I <= keywords.Length - 1; I++)
            {
                if ((KeywordInLine(TmpS, keywords(I) + " ") > -1) || ((KeywordInLine(TmpS, keywords(I)) > -1) && TmpS.EndsWith(keywords(I))))
                {
                    RetB = false;
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
            return(RetB);
        }
Пример #5
0
        private string PostParse(string vbSrc, PreParseResult preResult, string fixNamespaces)
        {
            string[] TmpLines;
            string   TmpLine;
            bool     TmpB = false;
            Int32    TmpStart;
            Int32    TmpI;
            string   TmpS;
            string   TmpS2;
            string   TmpS3;
            Int32    TmpBorderClassIndent = -1;
            bool     TmpInOperator        = false;
            bool     TmpInSelect          = false;
            string   RetS;

            //Replace TAB's with 2 spaces
            vbSrc = vbSrc.Replace(Chr(9), "  ");
            //repair string-append syntax 'str += "blabla"' --> 'str += "blabla"'
            vbSrc = vbSrc.Replace(" += \"", " += \"");
            //get all lines of code
            if (vbSrc.IndexOf(vbCrLf) > -1)
            {
                //Linebreak by CR-LF
                TmpLines = Split(vbSrc, vbCrLf);
            }
            else
            {
                //Linebreak by LF
                TmpLines = Split(vbSrc, vbLf);
            }
            //process each line (fix parser bugs)
            for (Int32 Y = 0; Y <= TmpLines.Length - 1; Y++)
            {
                if (ProviderHasParserFlag(ProviderParserFlags.HandleICSharpCodeParserBugs))
                {
                    //repair Parser-Bug "wrong enumumeration declarations" (remove incorrect 'private' prefix)
                    if (TmpB)
                    {
                        if (TmpLines(Y).ToLower.IndexOf("end enum") > -1)
                        {
                            TmpB = false;
                        }
                        else
                        {
                            TmpLines(Y) = TmpLines(Y).Replace("Private ", "");
                        }
                    }
                    else
                    {
                        if (KeywordInLine(TmpLines(Y), "enum ") > -1)
                        {
                            TmpB = true;
                        }
                    }
                    //repair Parser-Bug "wrong indexvariable incrementation in 'while' and 'for' statements"
                    //System.Math.Min(System.Threading.Interlocked.Increment(I),I-1) -->  I += 1
                    TmpStart = TmpLines(Y).ToLower.IndexOf("system.math.min(system.threading.interlocked.increment(");
                    if (TmpStart > -1)
                    {
                        TmpS = TmpLines(Y).Substring(TmpStart + 55);
                        TmpI = TmpS.IndexOf(")");
                        if (TmpI > -1)
                        {
                            //store code after statement in TmpS2
                            if (TmpS.Length > (5 + (TmpI * 2)))
                            {
                                TmpS2 = TmpS.Substring((5 + (TmpI * 2)), TmpS.Length - (5 + (TmpI * 2)));
                            }
                            else
                            {
                                TmpS2 = "";
                            }
                            //store I-variable in TmpS
                            TmpS = TmpS.Substring(0, TmpI);
                            //overwrite current line
                            TmpLines(Y) = TmpLines(Y).Substring(0, TmpStart) + TmpS + " += 1" + TmpS2;
                        }
                    }
                    //repair Parser-Bug "wrong explicit typecast"
                    //CType(ConversionHelpers.AsWorkaround(myvalue, GetType(mytype)), mytype) --> CType(myvalue, mytype)
                    TmpStart = TmpLines(Y).ToLower.IndexOf("ctype(conversionhelpers.asworkaround(");
                    if (TmpStart > -1)
                    {
                        TmpS = TmpLines(Y).Substring(TmpStart + 37);
                        TmpI = TmpS.IndexOf(",");
                        if (TmpI > -1)
                        {
                            //store value in TmpS2
                            TmpS2 = TmpS.Substring(0, TmpI);
                            TmpI  = TmpS.IndexOf("(");
                            if (TmpI > -1)
                            {
                                TmpS = TmpS.Substring(TmpI, TmpS.Length - TmpI);
                                TmpI = TmpS.IndexOf(")");
                                if (TmpI > -1)
                                {
                                    //store code after statement in TmpS3
                                    if (TmpS.Length > (5 + (TmpI * 2)))
                                    {
                                        TmpS3 = TmpS.Substring((5 + (TmpI * 2)), TmpS.Length - (5 + (TmpI * 2)));
                                    }
                                    else
                                    {
                                        TmpS3 = "";
                                    }
                                    //store type in TmpS
                                    TmpS = TmpS.Substring(0, TmpI);
                                    //overwrite current line
                                    TmpLines(Y) = TmpLines(Y).Substring(0, TmpStart) + "CType(" + TmpS2 + ", " + TmpS + ")" + TmpS3;
                                }
                            }
                        }
                    }
                    //repair Parser-Bug "multiple attributes on multiple lines have only one < on first and one > on last line"
                    //<MyAttrX()> _[CRLF]<MyAttrY()> _[CRLF]<MyAttrZ()>  -->  <MyAttrX(), _[CRLF] MyAttrY(), _[CRLF] MyAttrZ()>
                    if ((TmpLines(Y).Length > 3) && (TmpLines(Y).Substring(TmpLines(Y).Length - 3, 3) == "> _") && (TmpLines.Length > Y + 1))
                    {
                        //store next line without leading SPACE and TAB in TmpLine
                        TmpLine = StrTrimmLeft(StrTrimmLeft(TmpLines(Y + 1), Chr(9).ToString), " ");
                        if ((TmpLine.Length > 1) && (TmpLine.Substring(0, 1) == "<"))
                        {
                            //preserve Spaces before TmpLine in TmpS
                            if (TmpLines(Y + 1).Length > TmpLine.Length)
                            {
                                TmpS = TmpLines(Y + 1).Substring(0, TmpLines(Y + 1).Length - TmpLine.Length);
                            }
                            else
                            {
                                TmpS = "";
                            }
                            //replace "> _" with ", _" at end of this line
                            TmpLines(Y) = TmpLines(Y).Substring(0, TmpLines(Y).Length - 3) + ", _";
                            //replace "<" with " " at begin of next line
                            TmpLines(Y + 1) = TmpS + " " + TmpLine.Substring(1, TmpLine.Length - 1);
                        }
                    }
                }
            }
            //Lines -> String (Add Prefix: CRLF)
            vbSrc = vbCrLf + Join(TmpLines, vbCrLf);
            //fix Namespaces (remove)
            if (fixNamespaces != "")
            {
                foreach (string ns in Split(fixNamespaces, ","))
                {
                    vbSrc = vbSrc.Replace(vbCrLf + "Imports " + ns + vbCrLf, vbCrLf);
                }
            }
            //get all lines of code: String -> Lines (Remove Prefix: CRLF)
            TmpLines = Split(vbSrc.Substring(2, vbSrc.Length - 2), vbCrLf);
            //process each line (restore comments and regions)
            for (Int32 I = 0; I <= TmpLines.Length - 1; I++)
            {
                if ((TmpBorderClassIndent > -1) && (TmpLines(I).Length >= TmpBorderClassIndent + 9) && (TmpLines(I).Substring(TmpBorderClassIndent, 9) == "End Class"))
                {
                    //remove end of BorderClass
                    TmpBorderClassIndent = -1;
                    //mark line with KILL if its not last line
                    if ((TmpLines.Length > I + 1))
                    {
                        TmpLines(I) = "KILL";
                    }
                }
                else if (TmpLines(I).IndexOf("Class BorderClass") > -1)
                {
                    //remove begin of BorderClass
                    TmpLine = StrTrimmLeft(TmpLines(I), " ", true);
                    TmpBorderClassIndent = TmpLines(I).Length - TmpLine.Length;
                    TmpLines(I)          = "KILL";
                    if ((I > 0) && (TmpLines(I - 1) == ""))
                    {
                        //remove empty line before BorderClass (mark with KILL)
                        TmpLines(I - 1) = "KILL";
                    }
                    //mark next line after BorderClass with KILL if its empty and not last line
                    if ((TmpLines.Length > I + 1) && (TmpLines(I + 1) == ""))
                    {
                        TmpLines(I + 1) = "KILL";
                    }
                }
                else
                {
                    if ((TmpBorderClassIndent > -1) && (TmpLines(I).Length > 2) && (StrTrimmLeft(TmpLines(I).Substring(0, 2), " ") == ""))
                    {
                        //correct indention (remove 2 spaces because of BorderClass)
                        TmpLines(I) = TmpLines(I).Substring(2, TmpLines(I).Length - 2);
                    }
                    //get line without leading spaces
                    TmpLine = StrTrimmLeft(TmpLines(I), " ", true);
                    //preserve spaces
                    TmpS = TmpLines(I).Substring(0, TmpLines(I).Length - TmpLine.Length);
                    if (TmpLine.Length > 0)
                    {
                        //skip empty lines
                        if ((TmpLine.Length > 25) && (TmpLine.Substring(0, 25).ToLower == "dim commentvar as string "))
                        {
                            //convert comment
                            TmpLines(I) = TmpS + CSharpToVbComment(TmpLine.Substring(28, TmpLine.Length - 29));
                            //mark next line with KILL if its empty and not last line
                            if ((TmpLines.Length > I + 1) && (TmpLines(I + 1) == ""))
                            {
                                TmpLines(I + 1) = "KILL";
                            }
                        }
                        else if ((TmpLine.Length > 29) && (TmpLine.Substring(0, 29).ToLower == "private commentvar as string "))
                        {
                            //convert comment
                            TmpLines(I) = TmpS + CSharpToVbComment(TmpLine.Substring(32, TmpLine.Length - 33));
                            //mark next line with KILL if its empty and not last line
                            if ((TmpLines.Length > I + 1) && (TmpLines(I + 1) == ""))
                            {
                                TmpLines(I + 1) = "KILL";
                            }
                        }
                        else if ((TmpLine.Length > 24) && (TmpLine.Substring(0, 24).ToLower == "dim regionvar as string "))
                        {
                            //convert region or compilerarg
                            TmpLines(I) = CSharpToVbComment(TmpLine.Substring(27, TmpLine.Length - 28));
                        }
                        else if ((TmpLine.Length > 28) && (TmpLine.Substring(0, 28).ToLower == "private regionvar as string "))
                        {
                            //convert region or compilerarg
                            TmpLines(I) = CSharpToVbComment(TmpLine.Substring(31, TmpLine.Length - 32));
                        }
                        else if ((TmpLine // ERROR: Unknown binary operator Like
                                  ))
                        {
                            //Empty Line
                            TmpLines(I) = "";
                        }
                        else if (TmpLine.IndexOf("") > -1)
                        {
                            //restore Partial Class (new .NET 2.0 feature)
                            TmpLines(I) = TmpLines(I).Replace("", "").Replace("Class ", "Partial Class ");
                            //'restore Partial Class (new .NET 2.0 feature)
                            //TmpLines(i) = TmpS & "Partial " & TmpLine.Replace("PartialClassConverted", "")
                        }
                        else if (TmpLine.IndexOf("Operator_") > -1)
                        {
                            //restore "Operator Overloading" Part1 (new .NET 2.0 feature for VB.NET)
                            for (Int32 iOp = 0; iOp <= FOverloadableOperators.Length - 1; iOp++)
                            {
                                TmpLines(I)   = TmpLines(I).Replace("Function Operator_" + iOp.ToString, "Operator " + FOverloadableOperators(iOp));
                                TmpInOperator = true;
                            }
                        }
                        else if (TmpLine // ERROR: Unknown binary operator Like
                                 )
                        {
                            //begin of SELECT CASE
                            TmpInSelect = true;
                        }
                        else if ((TmpLine // ERROR: Unknown binary operator Like
                                  ) && TmpInSelect)
                        {
                            //inside SELECT CASE
                            //delete EXIT SELECT, if next statement is CASE
                            if (NextCodeLineHasNoneOfKeywords(TmpLines, I, new string[] { "Case" }))
                            {
                                TmpLines(I) = "KILL";
                            }
                        }
                        else if ((TmpLine == "End Select") && TmpInSelect)
                        {
                            //end of SELECT CASE
                            TmpInSelect = false;
                        }
                        else if ((TmpLine == "End Function") && TmpInOperator)
                        {
                            //restore "Operator Overloading" Part2 (new .NET 2.0 feature for VB.NET)
                            //End Function -> End Operator
                            TmpLines(I) = TmpS + "End Operator";
                        }
                        else if (TmpLine // ERROR: Unknown binary operator Like
                                 )
                        {
                            //repair compiration of Nothing (must be "is" instead of "=")
                            TmpLines(I) = TmpS + TmpLine.Replace("= Nothing", "Is Nothing");
                        }
                        else if (TmpLine // ERROR: Unknown binary operator Like
                                 )
                        {
                            //repair compiration of Not-Nothing (must be "is" instead of "=")
                            TmpLines(I) = TmpS + TmpLine.Replace("<> Nothing", "Is Nothing").Replace("If ", "If Not ");
                        }
                    }
                }
            }
            //get first lines comments and regions
            TmpS = "";
            for (Int32 I = 0; I <= preResult.FirstLinesComments.Length - 1; I++)
            {
                if (preResult.FirstLinesComments(I) != "")
                {
                    TmpS += CSharpToVbComment(preResult.FirstLinesComments(I)) + vbCrLf;
                }
            }
            //insert first lines at beginning
            RetS = TmpS + Join(TmpLines, vbCrLf) + vbCrLf;
            //remove all lines that are marked with       RetS = RetS.Replace("KILL" + vbCrLf, "");
            return(RetS);
        }