public GetOffset ( TextLocation location ) : int | ||
location | TextLocation | |
return | int |
public static BracketSearchResult SearchBrackets(TextDocument doc, int caretOffset, TextLocation caret) { var caretLocation = new CodeLocation(caret.Column, caret.Line); try { if (caretOffset < 1 || caretOffset>=doc.TextLength-2) return null; // Search backward DToken lastToken=null; var tk_start = SearchBackward(doc, caretOffset, caretLocation,out lastToken); if (tk_start == null) return null; // Search forward var tk_end = SearchForward(doc, doc.GetOffset(lastToken.EndLocation.Line,lastToken.EndLocation.Column), lastToken.EndLocation, getOppositeBracketToken(tk_start.Kind)); if (tk_end == null) return null; int start = doc.GetOffset(tk_start.Location.Line, tk_start.Location.Column), end = doc.GetOffset(tk_end.Location.Line, tk_end.Location.Column); return new BracketSearchResult(start, 1, end, 1); } catch { return null; } }
/// <summary> /// Create <see cref="NewFolding"/>s for the specified document. /// </summary> public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, XmlReader reader, out int firstErrorOffset) { Stack<XmlFoldStart> stack = new Stack<XmlFoldStart>(); List<NewFolding> foldMarkers = new List<NewFolding>(); try { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (!reader.IsEmptyElement) { XmlFoldStart newFoldStart = CreateElementFoldStart(document, reader); stack.Push(newFoldStart); } break; case XmlNodeType.EndElement: XmlFoldStart foldStart = stack.Pop(); CreateElementFold(document, foldMarkers, reader, foldStart); break; case XmlNodeType.Comment: CreateCommentFold(document, foldMarkers, reader); break; } } firstErrorOffset = -1; } catch (XmlException ex) { // ignore errors firstErrorOffset = document.GetOffset(ex.LineNumber, ex.LinePosition); } foldMarkers.Sort((a,b) => a.StartOffset.CompareTo(b.StartOffset)); return foldMarkers; }
public static BracketSearchResult SearchBrackets(TextDocument doc, IEditorData ed, IBlockNode curBlock, IStatement stmt) { while (stmt != null) { if (stmt is IExpressionContainingStatement) { var ecs = (IExpressionContainingStatement)stmt; foreach (var x in ecs.SubExpressions) { SurroundingParenthesesExpression spe = null; var xx = x; while (xx is ContainerExpression) { if (xx is SurroundingParenthesesExpression) spe = (SurroundingParenthesesExpression)xx; var subX = ((ContainerExpression)xx).SubExpressions; if (subX != null && subX.Length != 0) { xx = null; foreach (var sx in subX) if (ed.CaretLocation > sx.Location && ed.CaretLocation < sx.EndLocation) { xx = sx as ContainerExpression; break; } } } if (spe != null) return new BracketSearchResult( doc.GetOffset(spe.Location.Line, spe.Location.Column), 1, doc.GetOffset(spe.EndLocation.Line, spe.EndLocation.Column) - 1, 1); } } if (stmt is BlockStatement) return new BracketSearchResult( doc.GetOffset(stmt.Location.Line, stmt.Location.Column), 1, doc.GetOffset(stmt.EndLocation.Line, stmt.EndLocation.Column) - 1, 1); stmt = stmt.Parent; } if (curBlock != null && ed.CaretLocation < curBlock.BlockStartLocation) curBlock = curBlock.Parent as IBlockNode; if (curBlock == null || curBlock is DModule) return null; //TODO: Meta blocks, everything that could contain parentheses return new BracketSearchResult( doc.GetOffset(curBlock.BlockStartLocation.Line, curBlock.BlockStartLocation.Column), 1, doc.GetOffset(curBlock.EndLocation.Line, curBlock.EndLocation.Column) - 1, 1); }
public void GetOffset() { document.Text = "Hello,\nWorld!"; Assert.AreEqual(0, document.GetOffset(1, 1)); Assert.AreEqual(1, document.GetOffset(1, 2)); Assert.AreEqual(5, document.GetOffset(1, 6)); Assert.AreEqual(6, document.GetOffset(1, 7)); Assert.AreEqual(7, document.GetOffset(2, 1)); Assert.AreEqual(8, document.GetOffset(2, 2)); Assert.AreEqual(12, document.GetOffset(2, 6)); Assert.AreEqual(13, document.GetOffset(2, 7)); }
static int GetOffset(TextDocument document, XmlReader reader) { IXmlLineInfo info = reader as IXmlLineInfo; if (info != null && info.HasLineInfo()) { return document.GetOffset(info.LineNumber, info.LinePosition); } else { throw new ArgumentException("XmlReader does not have positioning information."); } }
/// <summary> /// Create an element fold if the start and end tag are on /// different lines. /// </summary> static void CreateElementFold(TextDocument document, List<NewFolding> foldMarkers, XmlReader reader, XmlFoldStart foldStart) { IXmlLineInfo lineInfo = (IXmlLineInfo)reader; int endLine = lineInfo.LineNumber; if (endLine > foldStart.StartLine) { int endCol = lineInfo.LinePosition + reader.Name.Length + 1; foldStart.EndOffset = document.GetOffset(endLine, endCol); foldMarkers.Add(foldStart); } }
/// <summary> /// Creates an XmlFoldStart for the start tag of an element. /// </summary> XmlFoldStart CreateElementFoldStart(TextDocument document, XmlReader reader) { // Take off 1 from the offset returned // from the xml since it points to the start // of the element name and not the beginning // tag. //XmlFoldStart newFoldStart = new XmlFoldStart(reader.Prefix, reader.LocalName, reader.LineNumber - 1, reader.LinePosition - 2); XmlFoldStart newFoldStart = new XmlFoldStart(); IXmlLineInfo lineInfo = (IXmlLineInfo)reader; newFoldStart.StartLine = lineInfo.LineNumber; newFoldStart.StartOffset = document.GetOffset(newFoldStart.StartLine, lineInfo.LinePosition - 1); if (this.ShowAttributesWhenFolded && reader.HasAttributes) { newFoldStart.Name = String.Concat("<", reader.Name, " ", GetAttributeFoldText(reader), ">"); } else { newFoldStart.Name = String.Concat("<", reader.Name, ">"); } return newFoldStart; }
public LineMarker(TextMarkerService svc, TextDocument doc, int beginLine, int endLine) { TextMarkerService = svc; StartOffset = doc.GetOffset(beginLine, 0); Length = doc.GetLineByNumber(endLine).EndOffset - StartOffset; }
public override IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset) { try { // Stopwatch sw = new Stopwatch(); // sw.Start(); List<NewFolding> foldMarkers = new List<NewFolding>(); firstErrorOffset = 0; if (dfd == null) return foldMarkers; // return foldMarkers; if (dfd.header.oline != -1 && dfd.header.cline != -1) foldMarkers.Add( new NewFolding(document.GetOffset(dfd.header.oline, 0), document.GetOffset(dfd.header.cline, document.Lines[dfd.header.cline - 1].TotalLength)) { Name = dfd.header.foldcaption }); if (dfd.wsl_oline != -1 && dfd.wsl_cline != -1) foldMarkers.Add( new NewFolding(document.GetOffset(dfd.wsl_oline, 0), document.GetOffset(dfd.wsl_cline, document.Lines[dfd.wsl_cline - 1].TotalLength)) { Name = "<weapon_strength_list>" }); int i = 0; for (i = 0; i < dfd.frames.Count - 1; i++) { if (i == 221) { } try { // continue; if (dfd.frames[i].oline == -1 || dfd.frames[i].cline == -1) continue; int start = document.GetOffset(dfd.frames[i].oline, 0); int end = document.GetOffset(dfd.frames[i].cline, document.Lines[dfd.frames[i].cline - 1].TotalLength); foldMarkers.Add(new NewFolding(start, end) { Name = dfd.frames[i].foldcaption }); } catch (Exception) { MessageBox.Show(i.ToString()); } } try { if (dfd.frames.Count != 0) if (dfd.frames[i].oline != -1 && dfd.frames[i].cline != -1) { int start2 = document.GetOffset(dfd.frames[i].oline, 0); int end2 = document.GetOffset(dfd.frames[i].cline, document.Lines[dfd.frames[i].cline - 1].TotalLength + 1); foldMarkers.Add(new NewFolding(start2, end2) { Name = dfd.frames[i].foldcaption }); } } catch (Exception) { throw; } for (i = 0; i < dfd.regions.Count; i++) { if (dfd.regions[i].oline == -1 || dfd.regions[i].cline == -1) continue; int start = document.GetOffset(dfd.regions[i].oline, 0); int end = document.GetOffset(dfd.regions[i].cline, document.Lines[dfd.regions[i].cline - 1].TotalLength); foldMarkers.Add(new NewFolding(start, end) { Name = dfd.regions[i].caption }); } foldMarkers.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset)); // sw.Stop(); // G.mainWindow.teOutput.AppendText // ("Fold/unfold - время прошло: " + sw.ElapsedMilliseconds + " миллисекунд" + Environment.NewLine); return foldMarkers; } catch (Exception ex) { new wException(ex).ShowDialog(); firstErrorOffset = 0; return null; } }
public static string GetWordUnderMouse(ICSharpCode.AvalonEdit.Document.TextDocument document, TextViewPosition position) { string wordHovered = string.Empty; var line = position.Line; var column = position.Column - 1; var offset = document.GetOffset(line, column); if (offset >= document.TextLength) { offset--; } var textAtOffset = document.GetText(offset, 1); // Get text backward of the mouse position, until the first space while (!string.IsNullOrWhiteSpace(textAtOffset)) { wordHovered = textAtOffset + wordHovered; offset--; if (offset < 0) { break; } textAtOffset = document.GetText(offset, 1); if (textAtOffset == "(" || textAtOffset == ")" || textAtOffset == "<" || textAtOffset == ">" || textAtOffset == "," || textAtOffset == ";") { break; } } // Get text forward the mouse position, until the first space offset = document.GetOffset(line, column); if (offset < document.TextLength - 1) { offset++; textAtOffset = document.GetText(offset, 1); while (!string.IsNullOrWhiteSpace(textAtOffset)) { wordHovered = wordHovered + textAtOffset; offset++; if (offset >= document.TextLength) { break; } textAtOffset = document.GetText(offset, 1); if (textAtOffset == "(" || textAtOffset == ")" || textAtOffset == "<" || textAtOffset == ">" || textAtOffset == "," || textAtOffset == ";") { break; } } } return(wordHovered); }
public int ToOffset(CodeLocation loc) { return(document.GetOffset(loc.Line, loc.Column)); }
private NewFolding CreateElementFold(TextDocument document, HtmlNode node) { NewFolding newFold = new NewFolding(); if (node.Line != node.EndNode.Line) { if (this.ShowAttributesWhenFolded && node.HasAttributes) newFold.Name = String.Concat("<", node.Name, " ", GetAttributeFoldText(node), ">"); else newFold.Name = String.Concat("<", node.Name, ">"); newFold.StartOffset = document.GetOffset(node.Line, node.LinePosition - 1); newFold.EndOffset = document.GetOffset(node.EndNode.Line, node.EndNode.LinePosition + node.OriginalName.Length + 3); } return newFold; }
public void SetEndPropOffset(TextDocument document, XmlReader reader) { if (_properties.Any()) { var prop = _properties.Last(); IXmlLineInfo lineInfo = (IXmlLineInfo)reader; prop.EndLine = lineInfo.LineNumber; prop.EndOffset = document.GetOffset(lineInfo.LineNumber, lineInfo.LinePosition + reader.Name.Length + 1); } }
public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, XmlReader reader, out int firstErrorOffset) { Stack<AmlFoldStart> stack = new Stack<AmlFoldStart>(); List<NewFolding> foldMarkers = new List<NewFolding>(); try { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (stack.Any() && stack.Peek().Name == "Item") { if (this.ShowAttributesWhenFolded && reader.LocalName == "id") { stack.Peek().SetKeyedName(reader.GetAttribute("keyed_name")); } else if (this.ShowAttributesWhenFolded && reader.LocalName == "keyed_name") { stack.Peek().SetKeyedName(reader.Value); } else if (this.ShowAttributesWhenFolded && reader.LocalName == "related_id") { stack.Peek().SetKeyedName("Related: " + (reader.GetAttribute("keyed_name") ?? "")); } if (reader.LocalName != "Relationships" && reader.LocalName != "related_id") { stack.Peek().SetStartPropOffset(document, reader); if (reader.IsEmptyElement) stack.Peek().SetEndPropOffset(document, reader); } } if (!reader.IsEmptyElement) { var newFoldStart = CreateElementFoldStart(document, reader); stack.Push(newFoldStart); } break; case XmlNodeType.EndElement: var foldStart = stack.Pop(); foldStart.Flush(); CreateElementFold(document, foldMarkers, reader, foldStart); if (stack.Any() && stack.Peek().Name == "Item" && reader.LocalName != "Relationships" && reader.LocalName != "related_id") { stack.Peek().SetEndPropOffset(document, reader); } break; case XmlNodeType.Comment: CreateCommentFold(document, foldMarkers, reader); break; } } firstErrorOffset = -1; } catch (XmlException ex) { // ignore errors at invalid positions (prevent ArgumentOutOfRangeException) if (ex.LineNumber >= 1 && ex.LineNumber <= document.LineCount) firstErrorOffset = document.GetOffset(ex.LineNumber, ex.LinePosition); else firstErrorOffset = 0; } foldMarkers.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset)); return foldMarkers; }
public void SetStartPropOffset(TextDocument document, XmlReader reader) { IXmlLineInfo lineInfo = (IXmlLineInfo)reader; if (!_properties.Any() || lineInfo.LineNumber > _properties.Last().EndLine + 1) { var prop = new AmlFoldStart(); prop.StartLine = lineInfo.LineNumber; prop.StartOffset = document.GetOffset(lineInfo.LineNumber, lineInfo.LinePosition - 1); _properties.Add(prop); } }