Beispiel #1
0
        private void SelectStringInTextDocument(StringResource stringResource)
        {
            try
            {
                if (stringResource == null)
                {
                    return;
                }

                string text = stringResource.Text;
                System.Drawing.Point location = stringResource.Location;
                //bool isAtString = false;

                TextDocument.Selection.MoveToLineAndOffset(location.X, location.Y, false);

                if (location.Y > 1)
                {
                    TextDocument.Selection.MoveToLineAndOffset(location.X, location.Y - 1, false);
                    TextDocument.Selection.CharRight(true, 1);
                    if (TextDocument.Selection.Text[0] != '@')
                    {
                        TextDocument.Selection.MoveToLineAndOffset(location.X, location.Y, false);
                    }

                    //  isAtString = true;
                }
                TextDocument.Selection.MoveToLineAndOffset(location.X, location.Y + text.Length + 2, true);

                if ((Window != null) && (Window != Dte.ActiveWindow))
                {
                    Window.Activate();
                }
            }
            catch (Exception e)
            {
                logger.Warn($"Problem with selecting string resource: {stringResource} in file: {ProjectItemFileName}");
                throw e;
            }
        }
        public static void SelectStringInTextDocument(TextDocument textDocument, StringResource stringResource)
        {
            if (stringResource == null)
            {
                return;
            }

            string text = stringResource.Text;

            System.Drawing.Point location = stringResource.Location;

            textDocument.Selection.MoveToLineAndOffset(location.X, location.Y, false);

            if (location.Y > 1)
            {
                textDocument.Selection.MoveToLineAndOffset(location.X, location.Y - 1, false);
                textDocument.Selection.CharRight(true, 1);
                if (textDocument.Selection.Text[0] != '@')
                {
                    textDocument.Selection.MoveToLineAndOffset(location.X, location.Y, false);
                }
            }
            textDocument.Selection.MoveToLineAndOffset(location.X, location.Y + text.Length + 2, true);
        }
        public bool Start()
        {
            try
            {
                if (!ProjectItem.IsOpen)
                {
                    ProjectItem.Open();
                }

                Document     document           = ProjectItem.Document;
                TextDocument textDocument       = document.Object(Constants.TEXT_DOCUMENT) as TextDocument;
                TextPoint    startPoint         = textDocument.StartPoint;
                TextPoint    endPoint           = textDocument.EndPoint;
                int          lastDocumentLength = textDocument.EndPoint.Line;

                logger.Debug("Start - parsing document: " + FileName);

                List <StringResource> stringResources = new List <StringResource>();

                bool isFullDocument          = startPoint.AtStartOfDocument && endPoint.AtEndOfDocument,
                     isTextWithStringLiteral = true;
                int startLine      = startPoint.Line;
                int startCol       = startPoint.LineCharOffset;
                int endLine        = endPoint.Line;
                int endCol         = endPoint.LineCharOffset;
                int documentLength = endPoint.Parent.EndPoint.Line;
                int insertIndex    = 0;

                if (isFullDocument)
                {
                    StringResources.Clear();
                }
                else
                {
                    //determine whether the text between startLine and endLine (including) contains double quotes
                    EditPoint editPoint = startPoint.CreateEditPoint() as EditPoint2;
                    if (!startPoint.AtStartOfLine)
                    {
                        editPoint.StartOfLine();
                    }
                    isTextWithStringLiteral = editPoint.GetLines(startLine, endLine + 1).Contains("\"");

                    //move trailing locations behind changed lines if needed and
                    //remove string resources on changed lines
                    int lineOffset = documentLength - lastDocumentLength;

                    for (int i = StringResources.Count - 1; i >= 0; --i)
                    {
                        StringResource stringResource = StringResources[i];
                        int            lineNo         = stringResource.Location.X;

                        if (lineNo + lineOffset > endLine)
                        {
                            if (lineOffset != 0)
                            {
                                stringResource.Offset(lineOffset, 0); //move
                            }
                        }
                        else if (lineNo >= startLine)
                        {
                            StringResources.RemoveAt(i); //remove changed line
                        }
                        else if (insertIndex == 0)
                        {
                            insertIndex = i + 1;
                        }
                    }
                }

                if (isTextWithStringLiteral)
                {
                    CodeElements elements = ProjectItem.FileCodeModel.CodeElements;

                    foreach (CodeElement element in elements)
                    {
                        ParseForStrings(element, stringResources, isCsharp, m_Settings, startLine, endLine);
                    }

                    if (isFullDocument)
                    {
                        StringResources.AddRange(stringResources);
                    }
                    else if (stringResources.Count > 0)
                    {
                        StringResources.InsertRange(insertIndex, stringResources);
                    }
                }

                logger.Debug("End - parsing document: " + FileName);

                return(true);
            } catch (Exception e)
            {
                logger.Warn(e, "An error occurred while parsing file: " + FileName);
                return(false);
            }
        }
Beispiel #4
0
        public void WriteToResource(StringResource stringResource)
        {
            try
            {
                SelectStringInTextDocument(stringResource);

                #endregion
                string comment = string.Empty;

                string name  = stringResource.Name;
                string value = stringResource.Text;

                if (TextDocument.Selection.Text.Length > 0 && TextDocument.Selection.Text[0] == '@')
                {
                    value = value.Replace("\"\"", "\\\"");
                }

                //add to the resource file (checking for duplicate)
                if (!AppendStringResource(ResxFileName, ref name, value, comment))
                {
                    return;
                }

                // CreateDesignerClass();

                if (string.IsNullOrEmpty(TextDocument.Selection.Text))
                {
                    return;
                }

                //get the length of the selected string literal and replace by resource call
                int replaceLength = TextDocument.Selection.Text.Length;


                string resourceCall = string.Concat(AliasName, ".", name);
                if (IsGlobalResourceFile)
                {
                    //standard global resource file
                    AliasName    = string.Concat("Glbl", AliasName);
                    resourceCall = string.Concat("Glbl", resourceCall);
                }

                int oldRow = TextDocument.Selection.ActivePoint.Line;

                if (IsDontUseResourceUsingAlias)
                {
                    //create a resource call like "Properties.SRB_Strings_Resources.myResText", "Properties.Resources.myResText", "Resources.myResText"
                    int lastDotPos = Namespace.LastIndexOf('.');

                    string resxNameSpace = lastDotPos >= 0 ?
                                           string.Concat(Namespace.Substring(lastDotPos + 1), ".") :
                                           string.Empty;

                    resourceCall = string.Concat(resxNameSpace, ClassName, ".", name);
                }

                TextDocument.Selection.Insert(
                    resourceCall, (int)vsInsertFlags.vsInsertFlagsContainNewText);
            }
            catch (Exception e)
            {
                string logMessage = $"Unable write string to resource file. File: {ProjectItemFileName}, resource: {stringResource}.";
                logger.Error(logMessage);
                throw new Exception(logMessage, e);
            }
        }