Beispiel #1
0
        /// <summary>
        /// Builds the comment stub and inserts it into the editor.
        /// </summary>
        /// <param name="position">The position of the last slash.</param>
        private void CreateStub(int position, ITextChange change)
        {
            string text = this._view.TextSnapshot.ToString();

            using (ITextEdit editor = this._view.TextBuffer.CreateEdit())
            {
                try
                {
                    this.tabs = StubUtils.GetIndention(position, this._view.TextSnapshot);
                    string summaryTag  = generateSummaryTag();
                    string parameters  = getFunctionParameters(position);
                    string returnTag   = getReturnTag(position);
                    string autoComment = summaryTag + parameters + returnTag;

                    int  lineStart     = this._view.TextSnapshot.GetLineFromPosition(position).Start.Position;
                    var  caretPosition = autoComment.IndexOf(StubUtils.CaretPlaceholder);
                    Span firstLineSpan = new Span(lineStart, change.NewSpan.End - lineStart);
                    autoComment = autoComment.Replace(StubUtils.CaretPlaceholder, "");

                    editor.Replace(firstLineSpan, autoComment);
                    ITextSnapshotLine prevLine = this._view.TextSnapshot.GetLineFromPosition(position);

                    StubUtils.MoveCaretAfterChange(this._view, this.editor, lineStart + caretPosition);

                    var after = editor.Apply();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(errorMsgPrefix + ex.Message);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns a string for a return tag if one is necessary.
        /// </summary>
        /// <param name="position">Position of the last slash in the triple slash comment</param>
        /// <returns>Return tag line as a string.</returns>
        private string getReturnTag(int position)
        {
            string result       = "";
            bool   shouldCreate = StubUtils.ShouldCreateReturnTag(position, this._view.TextSnapshot);

            if (shouldCreate)
            {
                result = NewLine() + createReturnString();
            }

            return(result);
        }
Beispiel #3
0
        private string getFunctionParameters(int position)
        {
            var parameters = StubUtils.GetFunctionParameters(position, this._view.TextSnapshot, true);
            var result     = "";

            foreach (var param in parameters)
            {
                if (!String.IsNullOrEmpty(param.Name))
                {
                    result += NewLine() + createParamString(param.Name, param.Type);
                }
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Returns a string for a return tag if one is necessary.
        /// </summary>
        /// <param name="position">Position of the last slash in the triple slash comment</param>
        /// <returns>Return tag line as a string.</returns>
        private string getReturnTag(int position)
        {
            string result       = "";
            bool   shouldCreate = StubUtils.ShouldCreateReturnTag(position, this._view.TextSnapshot, true);

            if (shouldCreate)
            {
                result = NewLine() + "@returns";

                if (contentType.Equals("JavaScript"))
                {
                    // Add {type} for JavaScript doc.
                    result += " {type} ";
                }
            }

            return(result);
        }
Beispiel #5
0
        private void CreateStub(int position, ITextChange change)
        {
            string text = this._view.TextSnapshot.ToString();

            using (ITextEdit editor = this._view.TextBuffer.CreateEdit())
            {
                try
                {
                    this.tabs = StubUtils.GetIndention(position, this._view.TextSnapshot, true);
                    string summaryString = StubUtils.Options.MultiLineSummary ? NewLine() : "";
                    string parameters    = getFunctionParameters(position);
                    string returnTag     = getReturnTag(position);
                    string commentBody   = summaryString + StubUtils.CaretPlaceholder + parameters + returnTag;
                    string autoComment   = this.tabs + "/**" + commentBody;

                    if (!String.IsNullOrEmpty(commentBody))
                    {
                        autoComment += Environment.NewLine + this.tabs;
                    }

                    autoComment += " */";

                    int lineStart     = this._view.TextSnapshot.GetLineFromPosition(position).Start.Position;
                    var caretPosition = autoComment.IndexOf(StubUtils.CaretPlaceholder);
                    autoComment = autoComment.Replace(StubUtils.CaretPlaceholder, "");

                    Span firstLineSpan = new Span(lineStart, change.NewSpan.End - lineStart);
                    editor.Replace(firstLineSpan, autoComment);

                    StubUtils.MoveCaretAfterChange(this._view, this.editor, lineStart + caretPosition);

                    var after = editor.Apply();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(errorMsgPrefix + ex.Message);
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// Returns true if the line at the given position ends with the /* characters prior to any pending changes.
 /// </summary>
 /// <param name="i"></param>
 /// <returns></returns>
 private bool LineIsJSDocOpening(int i)
 {
     return(StubUtils.GetLineTextFromPosition(i, this._view.TextSnapshot).EndsWith("/*"));
 }
Beispiel #7
0
 /// <summary>
 /// Returns true if the line at the given position ends with a double slash prior to any pending changes.
 /// </summary>
 /// <param name="i"></param>
 /// <returns></returns>
 private bool EndsInDoubleSlash(int i)
 {
     return(StubUtils.GetLineTextFromPosition(i, this._view.TextSnapshot).EndsWith("//"));
 }
Beispiel #8
0
        public static Parameter[] GetFunctionParameters(int position, ITextSnapshot capture, bool isAboveFunction = false)
        {
            int openFunctionLine = capture.GetLineNumberFromPosition(position - 1);

            if (isAboveFunction)
            {
                openFunctionLine += 1;
            }
            else
            {
                openFunctionLine -= 1;
            }

            ITextSnapshotLine line    = capture.GetLineFromLineNumber(openFunctionLine);
            string            curLine = line.Extent.GetText();

            openFunctionLine = StubUtils.GetFunctionDeclarationLineNumber(capture, openFunctionLine, isAboveFunction);
            //Not immediately after a function declaration
            if (openFunctionLine == -1)
            {
                return(new Parameter[0]);
            }

            curLine = capture.GetLineFromLineNumber(openFunctionLine).GetText();

            int ftnIndex           = StubUtils.javaScriptFnRegex.Match(curLine).Index;
            int firstParenPosition = -1;

            if (curLine.IndexOf('(', ftnIndex) > -1)
            {
                firstParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start +
                                     curLine.IndexOf('(', ftnIndex) + 1;
            }
            else
            {
                do
                {
                    openFunctionLine++;
                    curLine = capture.GetLineFromLineNumber(openFunctionLine).GetText();
                } while (!curLine.Contains("("));

                firstParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start
                                     + curLine.IndexOf('(')
                                     + 1;
            }

            var parenBlock = GetCompleteParenBlock(capture, openFunctionLine, firstParenPosition);

            if (parenBlock == null)
            {
                return(new Parameter[0]);
            }

            parenBlock = RemoveComments(parenBlock);
            return(parenBlock
                   .Split(',')
                   .Select(param =>
            {
                var result = Parameter.Parse(param);
                if (StubUtils.contentTypeName.Equals("JavaScript"))
                {
                    result.Type = "type";
                }

                return result;
            })
                   .ToArray());
        }