Пример #1
0
        // Returns the index of the parameter where the cursor is currently positioned.
        // -1 means the cursor is outside the method parameter list
        // 0 means no parameter entered
        // > 0 is the index of the parameter (1-based)
        public int GetCurrentParameterIndex(ICompletionWidget widget, CodeCompletionContext ctx)
        {
            int cursor = widget.CreateCodeCompletionContext().TriggerOffset;
            int i = ctx.TriggerOffset;
            //if (i < 0 || i >= editor.Length || editor.GetCharAt (i) == ')')
            //	return -1;

            if (i > cursor)
                return -1;
            else if (i == cursor)
                return 0;

            int parameterIndex = 1;

            while (i++ < cursor) {
                if (i >= widget.TextLength)
                    break;
                char ch = widget.GetChar (i);
                if (ch == ',')
                    parameterIndex++;
                else if (ch == ')')
                    return -1;
            }

            return parameterIndex;
        }
        // Returns the index of the parameter where the cursor is currently positioned.
        // -1 means the cursor is outside the method parameter list
        // 0 means no parameter entered
        // > 0 is the index of the parameter (1-based)
        internal int GetCurrentParameterIndex(ICompletionWidget widget, CodeCompletionContext ctx)
        {
            int cursor = widget.CurrentCodeCompletionContext.TriggerOffset;
            int i      = ctx.TriggerOffset;

            if (i < 0 || i >= editor.Length || editor.GetCharAt(i) == ')')
            {
                return(-1);
            }

            if (i > cursor)
            {
                return(-1);
            }
            else if (i == cursor)
            {
                return(0);
            }

            int parameterIndex = 1;

            while (i++ < cursor)
            {
                if (i >= widget.TextLength)
                {
                    break;
                }
                char ch = widget.GetChar(i);
                if (ch == ',')
                {
                    parameterIndex++;
                }
                else if (ch == ')')
                {
                    return(-1);
                }
            }

            return(parameterIndex);
        }
Пример #3
0
        internal static int GetCurrentParameterIndex(ICompletionWidget widget, int offset, int memberStart)
        {
            int cursor = widget.CurrentCodeCompletionContext.TriggerOffset;
            int i      = offset;

            if (i > cursor)
            {
                return(-1);
            }
            if (i == cursor)
            {
                return(1);
            }

            int index = memberStart + 1;
            int depth = 0;

            do
            {
                char c = widget.GetChar(i - 1);

                if (c == ',' && depth == 1)
                {
                    index++;
                }
                if (c == '<')
                {
                    depth++;
                }
                if (c == '>')
                {
                    depth--;
                }
                i++;
            } while (i <= cursor && depth > 0);

            return(depth == 0 ? -1 : index);
        }
		internal static int GetCurrentParameterIndex (ICompletionWidget widget, int offset, int memberStart)
		{
			int cursor = widget.CurrentCodeCompletionContext.TriggerOffset;
			int i = offset;
			
			if (i > cursor)
				return -1;
			if (i == cursor)
				return 1;
			
			int index = memberStart + 1;
			int depth = 0;
			do {
				char c = widget.GetChar (i - 1);
				
				if (c == ',' && depth == 1)
					index++;
				if (c == '<')
					depth++;
				if (c == '>')
					depth--;
				i++;
			} while (i <= cursor && depth > 0);
			
			return depth == 0 ? -1 : index;
		}
		internal static int GetCurrentParameterIndex (ICompletionWidget widget, int offset, int memberStart)
		{
			int cursor = widget.CurrentCodeCompletionContext.TriggerOffset;
			int i = offset;
			
			if (i > cursor)
				return -1;
			if (i == cursor) 
				return 1; // parameters are 1 based
			IEnumerable<string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType);
			CSharpIndentEngine engine = new CSharpIndentEngine (MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<CSharpFormattingPolicy> (types));
			int index = memberStart + 1;
			int parentheses = 0;
			int bracket = 0;
			do {
				char c = widget.GetChar (i - 1);
				engine.Push (c);
				switch (c) {
				case '{':
					if (!engine.IsInsideOrdinaryCommentOrString)
						bracket++;
					break;
				case '}':
					if (!engine.IsInsideOrdinaryCommentOrString)
						bracket--;
					break;
				case '(':
					if (!engine.IsInsideOrdinaryCommentOrString)
						parentheses++;
					break;
				case ')':
					if (!engine.IsInsideOrdinaryCommentOrString)
						parentheses--;
					break;
				case ',':
					if (!engine.IsInsideOrdinaryCommentOrString && parentheses == 1 && bracket == 0)
						index++;
					break;
				}
				i++;
			} while (i <= cursor && parentheses >= 0);
			
			return parentheses != 1 || bracket > 0 ? -1 : index;
		}
Пример #6
0
        internal static int GetCurrentParameterIndex(ICompletionWidget widget, int offset, int memberStart)
        {
            int cursor = widget.CurrentCodeCompletionContext.TriggerOffset;
            int i      = offset;

            if (i > cursor)
            {
                return(-1);
            }
            if (i == cursor)
            {
                return(1);                // parameters are 1 based
            }
            IEnumerable <string> types  = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);
            CSharpIndentEngine   engine = new CSharpIndentEngine(MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <CSharpFormattingPolicy> (types));
            int index       = memberStart + 1;
            int parentheses = 0;
            int bracket     = 0;

            do
            {
                char c = widget.GetChar(i - 1);
                engine.Push(c);
                switch (c)
                {
                case '{':
                    if (!engine.IsInsideOrdinaryCommentOrString)
                    {
                        bracket++;
                    }
                    break;

                case '}':
                    if (!engine.IsInsideOrdinaryCommentOrString)
                    {
                        bracket--;
                    }
                    break;

                case '(':
                    if (!engine.IsInsideOrdinaryCommentOrString)
                    {
                        parentheses++;
                    }
                    break;

                case ')':
                    if (!engine.IsInsideOrdinaryCommentOrString)
                    {
                        parentheses--;
                    }
                    break;

                case ',':
                    if (!engine.IsInsideOrdinaryCommentOrString && parentheses == 1 && bracket == 0)
                    {
                        index++;
                    }
                    break;
                }
                i++;
            } while (i <= cursor && parentheses >= 0);

            return(parentheses != 1 || bracket > 0 ? -1 : index);
        }
Пример #7
0
        public int GetCurrentParameterIndex(ICompletionWidget widget, CodeCompletionContext ctx)
        {
            return(args.CurrentlyTypedArgumentIndex + 1);

            int cursor = widget.CurrentCodeCompletionContext.TriggerOffset;
            int i      = ctx.TriggerOffset;

            if (i > cursor)
            {
                return(-1);
            }
            if (i == cursor)
            {
                return(1);                // parameters are 1 based
            }
            //IEnumerable<string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType);
            //CSharpIndentEngine engine = new CSharpIndentEngine (MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<CSharpFormattingPolicy> (types));
            int index       = 0 + 1;
            int parentheses = 0;
            int bracket     = 0;

            do
            {
                char c = widget.GetChar(i - 1);
                switch (c)
                {
                case '{':
                    if (!DResolver.CommentSearching.IsInCommentAreaOrString(doc.Editor.Text, i - 1))
                    {
                        bracket++;
                    }
                    break;

                case '}':
                    if (!DResolver.CommentSearching.IsInCommentAreaOrString(doc.Editor.Text, i - 1))
                    {
                        bracket--;
                    }
                    break;

                case '(':
                    if (!DResolver.CommentSearching.IsInCommentAreaOrString(doc.Editor.Text, i - 1))
                    {
                        parentheses++;
                    }
                    break;

                case ')':
                    if (!DResolver.CommentSearching.IsInCommentAreaOrString(doc.Editor.Text, i - 1))
                    {
                        parentheses--;
                    }
                    break;

                case ',':
                    if (!DResolver.CommentSearching.IsInCommentAreaOrString(doc.Editor.Text, i - 1) && parentheses == 1 && bracket == 0)
                    {
                        index++;
                    }
                    break;
                }
                i++;
            } while (i <= cursor && parentheses >= 0);

            return(parentheses != 1 || bracket > 0 ? -1 : index);
        }
        /// <remarks>
        /// This method returns the expression before a specified offset.
        /// That method is used in code completion to determine the expression given
        /// to the parser for type resolve.
        /// </remarks>
        //public static string GetExpressionBeforeOffset(string text, int offset)
        public static string GetExpressionBeforeOffset(ICompletionWidget widget, int offset)
        {
            int origOffset = offset;

            while (offset - 1 > 0) {
                switch (widget.GetChar (offset - 1)) {
                    case '}':
                        goto done;
            //						break;
                    case ']':
                        offset = SearchBracketBackward(widget, offset - 2, '[',']');
                        break;
                    case ')':
                        offset = SearchBracketBackward(widget, offset - 2, '(',')');
                        break;
                    case '.':
                        --offset;
                        break;
                    case '"':
                        return "\"\"";
                    case '\'':
                        return "'a'";
                    case '>':
                        if (widget.GetChar (offset - 2) == '-') {
                            offset -= 2;
                            break;
                        }
                        goto done;
                    default:
                        if (Char.IsWhiteSpace (widget.GetChar (offset - 1))) {
                            --offset;
                            break;
                        }
                        int start = offset - 1;
                        if (!IsLetterDigitOrUnderscore (widget.GetChar (start))) {
                            goto done;
                        }

                        while (start > 0 && IsLetterDigitOrUnderscore (widget.GetChar(start - 1))) {
                            --start;
                        }

                        Console.WriteLine("{0} -- {1}", offset, start);
                        string word = widget.GetText (start, offset);
                        Console.WriteLine("word >{0}<", word);
                        switch (word) {
                            case "ref":
                            case "out":
                            case "in":
                            case "return":
                            case "throw":
                            case "case":
                                goto done;
                        }

                        if (word.Length > 0 && !IsLetterDigitOrUnderscore(word[0])) {
                            goto done;
                        }
                        offset = start;
                        break;
                }
            }
            done:
            //			Console.WriteLine("offset : {0} origOffset: {1}", offset, origOffset);
            return offset - origOffset > 0 ? widget.GetText(origOffset, offset).Trim() : "";
        }
        /*
        public static CharacterType GetCharacterType(char c)
        {
            if(IsLetterDigitOrUnderscore(c))
                return CharacterType.LetterDigitOrUnderscore;
            if(Char.IsWhiteSpace(c))
                return CharacterType.WhiteSpace;
            return CharacterType.Other;
        }

        public static int GetFirstNonWSChar(IDocument document, int offset)
        {
            while (offset < document.TextLength && Char.IsWhiteSpace(document.GetCharAt(offset))) {
                ++offset;
            }
            return offset;
        }

        public static int FindWordEnd(IDocument document, int offset)
        {
            LineSegment line   = document.GetLineSegmentForOffset(offset);
            int     endPos = line.Offset + line.Length;
            while (offset < endPos && IsLetterDigitOrUnderscore(document.GetCharAt(offset))) {
                ++offset;
            }

            return offset;
        }

        public static int FindWordStart(IDocument document, int offset)
        {
            LineSegment line = document.GetLineSegmentForOffset(offset);

            while (offset > line.Offset && !IsLetterDigitOrUnderscore(document.GetCharAt(offset - 1))) {
                --offset;
            }

            return offset;
        }

        // go forward to the start of the next word
        // if the cursor is at the start or in the middle of a word we move to the end of the word
        // and then past any whitespace that follows it
        // if the cursor is at the start or in the middle of some whitespace we move to the start of the
        // next word
        public static int FindNextWordStart(IDocument document, int offset)
        {
            int originalOffset = offset;
            LineSegment line   = document.GetLineSegmentForOffset(offset);
            int     endPos = line.Offset + line.Length;
            // lets go to the end of the word, whitespace or operator
            CharacterType t = GetCharacterType(document.GetCharAt(offset));
            while (offset < endPos && GetCharacterType(document.GetCharAt(offset)) == t) {
                ++offset;
            }

            // now we're at the end of the word, lets find the start of the next one by skipping whitespace
            while (offset < endPos && GetCharacterType(document.GetCharAt(offset)) == CharacterType.WhiteSpace) {
                ++offset;
            }

            return offset;
        }

        // go back to the start of the word we are on
        // if we are already at the start of a word or if we are in whitespace, then go back
        // to the start of the previous word
        public static int FindPrevWordStart(IDocument document, int offset)
        {
            int originalOffset = offset;
            LineSegment line = document.GetLineSegmentForOffset(offset);
            if (offset > 0) {
                CharacterType t = GetCharacterType(document.GetCharAt(offset - 1));
                while (offset > line.Offset && GetCharacterType(document.GetCharAt(offset - 1)) == t) {
                    --offset;
                }

                // if we were in whitespace, and now we're at the end of a word or operator, go back to the beginning of it
                if(t == CharacterType.WhiteSpace && offset > line.Offset) {
                    t = GetCharacterType(document.GetCharAt(offset - 1));
                    while (offset > line.Offset && GetCharacterType(document.GetCharAt(offset - 1)) == t) {
                        --offset;
                    }
                }
            }

            return offset;
        }

        public static string GetLineAsString(IDocument document, int lineNumber)
        {
            LineSegment line = document.GetLineSegment(lineNumber);
            return document.GetText(line.Offset, line.Length);
        }
        */
        static bool ScanLineComment(ICompletionWidget widget, int offset)
        {
            while (offset > 0 && offset < widget.TextLength) {
                char ch = widget.GetChar (offset);
                switch (ch) {
                    case '\r':
                    case '\n':
                        return false;
                    case '/':
                        if (widget.GetChar (offset + 1) == '/') {
                            return true;
                        }
                        break;
                }
                --offset;
            }
            return false;
        }
        public static int SearchBracketBackward(ICompletionWidget widget, int offset, char openBracket, char closingBracket)
        {
            // FIXME: use iters
            int brackets = -1;
            bool inString = false;
            bool inChar   = false;
            bool blockComment = false;

            while (offset >= 0 && offset < widget.TextLength) {
                char ch = widget.GetChar(offset);
                switch (ch) {
                    case '/':
                        if (blockComment) {
                            if (widget.GetChar(offset + 1)== '*') {
                                blockComment = false;
                            }
                        }
                        if (!inString && !inChar && offset + 1 < widget.TextLength) {
                            if (offset > 0 && widget.GetChar(offset - 1) == '*') {
                                blockComment = true;
                            }
                        }
                        break;
                    case '"':
                        if (!inChar && !blockComment && !ScanLineComment(widget, offset)) {
                            inString = !inString;
                        }
                        break;
                    case '\'':
                        if (!inString && !blockComment && !ScanLineComment(widget, offset)) {
                            inChar = !inChar;
                        }
                        break;
                    default :
                        if (ch == closingBracket) {
                            if (!(inString || inChar || blockComment) && !ScanLineComment(widget, offset)) {
                                --brackets;
                            }
                        } else if (ch == openBracket) {
                            if (!(inString || inChar || blockComment) && !ScanLineComment(widget, offset)) {
                                ++brackets;
                                if (brackets == 0) {
                                    return offset;
                                }
                            }
                        }
                        break;
                }
                --offset;
            }
            return - 1;
        }