public string GetText(CharPosition rangeStart, CharPosition rangeEnd) { int startX = rangeStart.GetCharacterPosition().X; int startY = rangeStart.GetCharacterPosition().Y; int endX = rangeEnd.GetCharacterPosition().X; int endY = rangeEnd.GetCharacterPosition().Y; return(GetText(startY, startX, endY, endX)); }
internal ScriptState(ITextBuffer textBuffer) { this.textBuffer = textBuffer; this.cursorPosition = new CharPosition(textBuffer); this.selectionStart = new CharPosition(textBuffer); this.selectionEnd = new CharPosition(textBuffer); // @TODO(Ben): Can we do without the following three lines? this.cursorPosition.SetCharacterPosition(new System.Drawing.Point(0, 0)); this.selectionStart.SetCharacterPosition(new System.Drawing.Point(0, 0)); this.selectionEnd.SetCharacterPosition(new System.Drawing.Point(0, 0)); }
internal string Format(CharPosition start, CharPosition end) { Paragraph paragraph = new Paragraph(); for (int lineIndex = start.CharacterY; lineIndex <= end.CharacterY; ++lineIndex) { string lineContent = textBuffer.GetLineContent(lineIndex); if (string.IsNullOrEmpty(lineContent)) continue; int charStart = 0, lineLength = lineContent.Length; if (lineIndex == start.CharacterY) // Starting line. charStart = start.CharacterX; if (lineIndex == end.CharacterY) // Ending line. lineLength = end.CharacterX; for (int charIndex = charStart; charIndex < lineLength; ++charIndex) { CodeFragment fragment = null; int fragmentWidth = codeFragmentManager.GetFragment( charIndex, lineIndex, out fragment); if (null == fragment) { // We have encountered a whitespace character (e.g. space, tab, etc), // scan forward until we get a non-whitespace character, then flush // whatever we have found (whitespaces) into the paragraph at one go. // int startIndex = charIndex; while (charIndex < lineLength) { fragmentWidth = codeFragmentManager.GetFragment( charIndex, lineIndex, out fragment); if (fragmentWidth > 0 && (null != fragment)) { string text = lineContent.Substring(startIndex, charIndex - startIndex); FormatFragment(null, text, paragraph); break; } charIndex = charIndex + 1; } if (charIndex >= lineLength) { // If there was no fragment at the time we reached the // end of line, then flush out the remaining whitespaces, // if there is any. // if (null == fragment && (startIndex < charIndex)) { FormatFragment(null, lineContent.Substring(startIndex, charIndex - startIndex), paragraph); } break; // Reach the end of line. } } int offsetToNextChar = fragmentWidth - 1; if ((charIndex + fragmentWidth) > (lineLength - 1)) { fragmentWidth = (lineLength - 1) - charIndex; offsetToNextChar = fragmentWidth - 1; } // Initialize the text store. string textContent = string.Empty; textContent = lineContent.Substring(charIndex, ((fragmentWidth == 0) ? 1 : fragmentWidth)); FormatFragment(fragment, textContent, paragraph); if (fragmentWidth > 0) charIndex += offsetToNextChar; } } FlowDocument flowDocument = new FlowDocument(); flowDocument.TextAlignment = TextAlignment.Left; flowDocument.FontSize = 11; flowDocument.FontFamily = new FontFamily("Consolas"); flowDocument.FlowDirection = FlowDirection.LeftToRight; flowDocument.TextAlignment = TextAlignment.Left; flowDocument.Blocks.Add(paragraph); TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); if (range.CanSave(DataFormats.Rtf)) { using (MemoryStream stream = new MemoryStream()) { range.Save(stream, DataFormats.Rtf); stream.Flush(); return (Encoding.UTF8.GetString(stream.ToArray())); } } return string.Empty; }
protected Rect CalculateRectangleFullLine(CharPosition start, int firstVisibleLine) { double x = 0, y = 0, width = 0; double vertDisplayOffset = firstVisibleLine * Configurations.FontDisplayHeight; double horzDisplayOffset = textEditorCanvas.FirstVisibleColumn * Configurations.FormatFontWidth; x = Configurations.CanvasMarginLeft - Configurations.ShadowWidth; y = start.ScreenY; width = textEditorCanvas.ActualWidth + textEditorCanvas.HorizontalOffset; if (width > 0) { y = y - vertDisplayOffset; x = x - horzDisplayOffset; return new Rect(x, y, width, Configurations.FontDisplayHeight); } return new Rect(); }
protected List<Rect> CalculateRectangles(CharPosition start, CharPosition end, int firstVisibleLine) { if (start.CharacterY > end.CharacterY) return null; else if (start.CharacterY == end.CharacterY) { if (start.CharacterX == end.CharacterX) return null; } List<Rect> results = new List<Rect>(); double vertDisplayOffset = firstVisibleLine * Configurations.FontDisplayHeight; double horzDisplayOffset = textEditorCanvas.FirstVisibleColumn * Configurations.FormatFontWidth; if (start.CharacterY == end.CharacterY) // Start and end are on the same line. { double x = start.ScreenX - horzDisplayOffset; double y = start.ScreenY - vertDisplayOffset; double width = end.ScreenX - start.ScreenX; results.Add(new Rect(x, y, width, Configurations.FontDisplayHeight)); return results; } ITextBuffer textBuffer = currentScript.GetTextBuffer(); for (int index = start.CharacterY; index <= end.CharacterY; ++index) { double x = 0, y = 0, width = 0; if (index == start.CharacterY) // If this is the first line. { x = start.ScreenX -Configurations.ShadowWidth; y = start.ScreenY; width = Configurations.FormatFontWidth * textBuffer.GetCharacterCount(index, true, true); width = (width - (start.ScreenX - Configurations.CanvasMarginLeft))+Configurations.ShadowWidth; } else if (index == end.CharacterY) // If this is the last line. { x = Configurations.CanvasMarginLeft - Configurations.ShadowWidth; y = end.ScreenY; width = end.ScreenX - Configurations.CanvasMarginLeft + Configurations.ShadowWidth; } else // All lines except the first and last lines. { x = Configurations.CanvasMarginLeft - Configurations.ShadowWidth; y = index * Configurations.FontDisplayHeight; width = Configurations.FormatFontWidth * textBuffer.GetCharacterCount(index, true, true) + Configurations.ShadowWidth; } if (width > 0) // Sorry babe, we're only interested in non-empty lines. { y = y - vertDisplayOffset; x = x - horzDisplayOffset; results.Add(new Rect(x, y, width, Configurations.FontDisplayHeight)); } } return results; }
protected override void UpdateLayerForScriptCore(IScriptObject hostScript) { selectionStart = hostScript.CreateCharPosition(); selectionEnd = hostScript.CreateCharPosition(); lineSelection = hostScript.CreateCharPosition(); inLineSelection = hostScript.CreateCharPosition(); allOccurencesSelectionStart = hostScript.CreateCharPosition(); allOccurencesSelectionEnd = hostScript.CreateCharPosition(); currentOccurenceSelectionStart = hostScript.CreateCharPosition(); currentOccurenceSelectionEnd = hostScript.CreateCharPosition(); }
private void UpdateVisualOffset(int column, int line) { CharPosition converter = Solution.Current.ActiveScript.CreateCharPosition(); scriptState.visualOffset = converter.CharToVisualOffset(line, column); }
internal string Format(CharPosition start, CharPosition end) { Paragraph paragraph = new Paragraph(); for (int lineIndex = start.CharacterY; lineIndex <= end.CharacterY; ++lineIndex) { string lineContent = textBuffer.GetLineContent(lineIndex); if (string.IsNullOrEmpty(lineContent)) { continue; } int charStart = 0, lineLength = lineContent.Length; if (lineIndex == start.CharacterY) // Starting line. { charStart = start.CharacterX; } if (lineIndex == end.CharacterY) // Ending line. { lineLength = end.CharacterX; } for (int charIndex = charStart; charIndex < lineLength; ++charIndex) { CodeFragment fragment = null; int fragmentWidth = codeFragmentManager.GetFragment( charIndex, lineIndex, out fragment); if (null == fragment) { // We have encountered a whitespace character (e.g. space, tab, etc), // scan forward until we get a non-whitespace character, then flush // whatever we have found (whitespaces) into the paragraph at one go. // int startIndex = charIndex; while (charIndex < lineLength) { fragmentWidth = codeFragmentManager.GetFragment( charIndex, lineIndex, out fragment); if (fragmentWidth > 0 && (null != fragment)) { string text = lineContent.Substring(startIndex, charIndex - startIndex); FormatFragment(null, text, paragraph); break; } charIndex = charIndex + 1; } if (charIndex >= lineLength) { // If there was no fragment at the time we reached the // end of line, then flush out the remaining whitespaces, // if there is any. // if (null == fragment && (startIndex < charIndex)) { FormatFragment(null, lineContent.Substring(startIndex, charIndex - startIndex), paragraph); } break; // Reach the end of line. } } int offsetToNextChar = fragmentWidth - 1; if ((charIndex + fragmentWidth) > (lineLength - 1)) { fragmentWidth = (lineLength - 1) - charIndex; offsetToNextChar = fragmentWidth - 1; } // Initialize the text store. string textContent = string.Empty; textContent = lineContent.Substring(charIndex, ((fragmentWidth == 0) ? 1 : fragmentWidth)); FormatFragment(fragment, textContent, paragraph); if (fragmentWidth > 0) { charIndex += offsetToNextChar; } } } FlowDocument flowDocument = new FlowDocument(); flowDocument.TextAlignment = TextAlignment.Left; flowDocument.FontSize = 11; flowDocument.FontFamily = new FontFamily("Consolas"); flowDocument.FlowDirection = FlowDirection.LeftToRight; flowDocument.TextAlignment = TextAlignment.Left; flowDocument.Blocks.Add(paragraph); TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); if (range.CanSave(DataFormats.Rtf)) { using (MemoryStream stream = new MemoryStream()) { range.Save(stream, DataFormats.Rtf); stream.Flush(); return(Encoding.UTF8.GetString(stream.ToArray())); } } return(string.Empty); }