예제 #1
0
        public static void GeneratePropertyFromVariable(CodeVariable2 variable, bool generateGetter, bool generateSetter, bool generateComments)
        {
            CodeClass2    codeClass     = variable.Collection.Parent as CodeClass2;
            CodeGenerator codeGenerator = CreateCodeGenerator(codeClass.Language);

            string propertyName = ConvertVariableNameToPropertyName(variable.Name);

            if (!ContainsMember(codeClass, propertyName))
            {
                string getterName = String.Empty;
                string setterName = String.Empty;

                if (generateGetter)
                {
                    getterName = propertyName;
                }
                if (generateSetter)
                {
                    setterName = propertyName;
                }

                CodeProperty property = (CodeProperty)codeClass.AddProperty(getterName, setterName, variable.Type, -1, vsCMAccess.vsCMAccessPublic, null);
                if (generateComments)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<doc>");
                    sb.AppendLine("<summary>");
                    sb.AppendLine();
                    sb.AppendLine("</summary>");
                    sb.Append("</doc>");

                    property.DocComment = sb.ToString();
                }

                if (generateGetter)
                {
                    EditPoint2 editPoint = (EditPoint2)property.Getter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                    editPoint.EndOfLine();
                    int position = editPoint.LineCharOffset;
                    editPoint.StartOfLine();
                    editPoint.Delete(position);
                    editPoint.Insert(codeGenerator.GenerateReturnStatement(variable.Name));
                    editPoint.SmartFormat(editPoint);
                }

                if (generateSetter)
                {
                    EditPoint2 editPoint = (EditPoint2)property.Setter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                    editPoint.Insert(codeGenerator.GenerateAssignStatement(variable.Name, "value"));
                    editPoint.SmartFormat(editPoint);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Adds context to the result item, coming from code block starting at given position
        /// </summary>
        protected void AddContextToItem(AbstractResultItem item, EditPoint2 editPoint)
        {
            StringBuilder context = new StringBuilder();
            // indices +1 !!

            int topLines = 0;

            int currentLine         = item.ReplaceSpan.iStartLine;
            int contextRelativeLine = 0;

            // add NumericConstants.ContextLineRadius lines above the result item with at least 2 non-whitespace characters
            while (currentLine >= 1 && topLines < NumericConstants.ContextLineRadius)
            {
                editPoint.MoveToLineAndOffset(currentLine, 1);
                string lineText = editPoint.GetText(editPoint.LineLength);
                if (lineText.Trim().Length > 0)
                {
                    context.Insert(0, lineText + Environment.NewLine);
                    contextRelativeLine++;
                    if (lineText.Trim().Length > 1)
                    {
                        topLines++;
                    }
                }
                currentLine--;
            }

            editPoint.MoveToLineAndOffset(item.ReplaceSpan.iStartLine + 1, 1);
            context.Append(editPoint.GetText(item.ReplaceSpan.iStartIndex));

            context.Append(StringConstants.ContextSubstituteText); // add text that will be displayed instead of actual result item

            editPoint.MoveToLineAndOffset(item.ReplaceSpan.iEndLine + 1, item.ReplaceSpan.iEndIndex + 1);
            context.Append(editPoint.GetText(editPoint.LineLength - item.ReplaceSpan.iEndIndex + 1));

            int botLines = 0;

            currentLine = item.ReplaceSpan.iEndLine + 2;
            // add NumericConstants.ContextLineRadius lines below the result item with at least 2 non-whitespace characters
            while (botLines < NumericConstants.ContextLineRadius)
            {
                editPoint.MoveToLineAndOffset(currentLine, 1);
                string lineText = editPoint.GetText(editPoint.LineLength);
                if (lineText.Trim().Length > 0)
                {
                    context.Append(Environment.NewLine + lineText);
                    if (lineText.Trim().Length > 1)
                    {
                        botLines++;
                    }
                }
                editPoint.EndOfLine();
                if (editPoint.AtEndOfDocument)
                {
                    break;
                }
                currentLine++;
            }

            item.Context             = context.ToString();
            item.ContextRelativeLine = contextRelativeLine; // index of "middle" line
        }