GetLineByOffset() public method

public GetLineByOffset ( int offset ) : Mono.TextEditor.DocumentLine
offset int
return Mono.TextEditor.DocumentLine
        // todo: move to version control backend
        IEnumerable <Conflict> Conflicts(Mono.TextEditor.TextDocument doc)
        {
            int mergeStart = 0;

            while ((mergeStart = doc.IndexOf("<<<<<<<", mergeStart, doc.TextLength - mergeStart, StringComparison.Ordinal)) >= 0)
            {
                DocumentLine start = doc.GetLineByOffset(mergeStart);
                if (start.Offset != mergeStart)
                {
                    continue;
                }
                int          dividerOffset = doc.IndexOf("=======", mergeStart, doc.TextLength - mergeStart, StringComparison.Ordinal);
                DocumentLine divider       = doc.GetLineByOffset(dividerOffset);

                int          endOffset = doc.IndexOf(">>>>>>>", dividerOffset, doc.TextLength - dividerOffset, StringComparison.Ordinal);
                DocumentLine end       = doc.GetLineByOffset(endOffset);
                mergeStart = dividerOffset + 1;

                yield return(new Conflict(new TextSegment(start.EndOffsetIncludingDelimiter, divider.Offset - start.EndOffsetIncludingDelimiter),
                                          new TextSegment(divider.EndOffsetIncludingDelimiter, end.Offset - divider.EndOffsetIncludingDelimiter),
                                          start,
                                          divider,
                                          end));
            }
        }
		static int ScanWord (TextDocument doc, int offset, bool forwardDirection)
		{
			if (offset < 0 || offset >= doc.TextLength)
				return offset;
			var line = doc.GetLineByOffset (offset);
			char first = doc.GetCharAt (offset);
			if (char.IsPunctuation (first))
				return forwardDirection ? System.Math.Min (line.Offset + line.Length, offset + 1) : System.Math.Max (line.Offset, offset - 1);
			while (offset >= line.Offset && offset < line.Offset + line.Length) {
				char ch = doc.GetCharAt (offset);
				if (char.IsWhiteSpace (first) && !char.IsWhiteSpace (ch)
				    || WordFindStrategy.IsNoIdentifierPart (first) && !WordFindStrategy.IsNoIdentifierPart (ch)
				    || (char.IsLetterOrDigit (first) || first == '_') && !(char.IsLetterOrDigit (ch) || ch == '_'))
					break;
				offset = forwardDirection ? offset + 1 : offset - 1;
			}
			return System.Math.Min (line.Offset + line.Length,
			                        System.Math.Max (line.Offset, offset + (forwardDirection ? 0 : 1)));
		}
		bool StartsInLineComment (TextDocument document, int offset)
		{
			List<string> lineComments = GetList (document, "LineComment");
			DocumentLine line = document.GetLineByOffset (offset);
			for (int i = line.Offset ; i < offset; i++) {
				if (StartsWithListMember (document, lineComments, i) >= 0)
					return true;
			}
			return false;
		}
		int GetLastSourceCodePosition (TextDocument document, int lineOffset)
		{
			DocumentLine line = document.GetLineByOffset (lineOffset);
			if (line == null)
				return lineOffset;
			bool isInBlockComment = false;
			bool isInLineComment  = false;
			int  curStringQuote   = -1;
			
			List<string> lineComments       = GetList (document, "LineComment");
			List<string> blockCommentStarts = GetList (document, "BlockCommentStart");
			List<string> blockCommentEnds   = GetList (document, "BlockCommentEnd");
			List<string> stringQuotes       = GetList (document, "StringQuote");
			
			for (int i = 0 ; i < line.Length; i++) {
				int offset = line.Offset + i;
				// check line comments
				if (!isInBlockComment && curStringQuote < 0) {
					isInLineComment = StartsWithListMember (document, lineComments, offset) >= 0;
					if (isInLineComment) 
						return System.Math.Min (offset, lineOffset);
				}
				// check block comments
				if (!isInLineComment && curStringQuote < 0) {
					if (!isInBlockComment) { 
						isInBlockComment = StartsWithListMember (document, blockCommentStarts, offset) >= 0;
					} else {
						isInBlockComment = StartsWithListMember (document, blockCommentEnds, offset) < 0;
					}
				}
				
				if (!isInBlockComment && !isInLineComment) {
					int j = StartsWithListMember (document, stringQuotes, offset);
					if (j >= 0) {
						if (curStringQuote >= 0) {
							if (curStringQuote == j)
								curStringQuote = -1;
						} else {
							curStringQuote = j;
						}
					}
				}
			}
			return lineOffset;
		}
Example #5
0
		// todo: move to version control backend
		static IEnumerable<Conflict> Conflicts (TextDocument doc)
		{
			int mergeStart = 0;
			while ((mergeStart = doc.IndexOf ("<<<<<<<", mergeStart, doc.TextLength - mergeStart, StringComparison.Ordinal)) >= 0) {
				DocumentLine start = doc.GetLineByOffset (mergeStart);
				if (start.Offset != mergeStart)
					continue;
				int dividerOffset = doc.IndexOf ("=======", mergeStart, doc.TextLength - mergeStart, StringComparison.Ordinal);
				DocumentLine divider = doc.GetLineByOffset (dividerOffset);

				int endOffset = doc.IndexOf (">>>>>>>", dividerOffset, doc.TextLength - dividerOffset, StringComparison.Ordinal);
				DocumentLine end = doc.GetLineByOffset (endOffset);
				mergeStart = dividerOffset + 1;

				yield return new Conflict (new TextSegment (start.EndOffsetIncludingDelimiter, divider.Offset - start.EndOffsetIncludingDelimiter),
					new TextSegment (divider.EndOffsetIncludingDelimiter, end.Offset - divider.EndOffsetIncludingDelimiter),
					start,
					divider,
					end);
			}
		}