本文整理汇总了C#中ITextSnapshot.GetLineNumberFromPosition方法的典型用法代码示例。如果您正苦于以下问题:C# ITextSnapshot.GetLineNumberFromPosition方法的具体用法?C# ITextSnapshot.GetLineNumberFromPosition怎么用?C# ITextSnapshot.GetLineNumberFromPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITextSnapshot
的用法示例。
在下文中一共展示了ITextSnapshot.GetLineNumberFromPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetLinesToAlign
IEnumerable<ITextSnapshotLine> GetLinesToAlign(ITextSnapshot snapshot)
{
int start = snapshot.GetLineNumberFromPosition(m_view.Selection.Start.Position);
int end = snapshot.GetLineNumberFromPosition(m_view.Selection.End.Position);
if (start == end)
{
start = 0;
end = snapshot.LineCount -1;
}
return start.UpTo(end).Select(x => snapshot.GetLineFromLineNumber(x));
}
示例2: CreateProjectionBuffer
private static IProjectionBuffer CreateProjectionBuffer(
IProjectionBufferFactoryService factoryService,
IContentTypeRegistryService registryService,
IEditorOptions editorOptions,
ITextSnapshot snapshot,
string separator,
object suffixOpt,
bool trim,
params LineSpan[] exposedLineSpans)
{
var spans = new List<object>();
if (exposedLineSpans.Length > 0)
{
if (exposedLineSpans[0].Start > 0 && !string.IsNullOrEmpty(separator))
{
spans.Add(separator);
spans.Add(editorOptions.GetNewLineCharacter());
}
var snapshotSpanRanges = CreateSnapshotSpanRanges(snapshot, exposedLineSpans);
var indentColumn = trim
? DetermineIndentationColumn(editorOptions, snapshotSpanRanges.Flatten())
: 0;
foreach (var snapshotSpanRange in snapshotSpanRanges)
{
foreach (var snapshotSpan in snapshotSpanRange)
{
var line = snapshotSpan.Snapshot.GetLineFromPosition(snapshotSpan.Start);
var indentPosition = line.GetLineOffsetFromColumn(indentColumn, editorOptions) + line.Start;
var mappedSpan = new SnapshotSpan(snapshotSpan.Snapshot,
Span.FromBounds(indentPosition, snapshotSpan.End));
var trackingSpan = mappedSpan.CreateTrackingSpan(SpanTrackingMode.EdgeExclusive);
spans.Add(trackingSpan);
// Add a newline between every line.
if (snapshotSpan != snapshotSpanRange.Last())
{
spans.Add(editorOptions.GetNewLineCharacter());
}
}
// Add a separator between every set of lines.
if (snapshotSpanRange != snapshotSpanRanges.Last())
{
spans.Add(editorOptions.GetNewLineCharacter());
spans.Add(separator);
spans.Add(editorOptions.GetNewLineCharacter());
}
}
if (snapshot.GetLineNumberFromPosition(snapshotSpanRanges.Last().Last().End) < snapshot.LineCount - 1)
{
spans.Add(editorOptions.GetNewLineCharacter());
spans.Add(separator);
}
}
if (suffixOpt != null)
{
if (spans.Count >= 0)
{
if (!separator.Equals(spans.Last()))
{
spans.Add(editorOptions.GetNewLineCharacter());
spans.Add(separator);
}
spans.Add(editorOptions.GetNewLineCharacter());
}
spans.Add(suffixOpt);
}
return factoryService.CreateProjectionBuffer(
projectionEditResolver: null,
sourceSpans: spans,
options: ProjectionBufferOptions.None,
contentType: registryService.GetContentType(ShaderPreviewContentType));
}
示例3: GetFunctionParameters
public static string[] 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 prevLine = line.Extent.GetText();
openFunctionLine = StubUtils.GetFunctionDeclarationLineNumber(capture, openFunctionLine, isAboveFunction);
//Not immediately after a function declaration
if (openFunctionLine == -1) return new string[0];
prevLine = capture.GetLineFromLineNumber(openFunctionLine).GetText();
int ftnIndex = StubUtils.javaScriptFnRegex.Match(prevLine).Index;
int firstParenPosition = -1;
if (prevLine.IndexOf('(', ftnIndex) > -1)
{
firstParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start +
prevLine.IndexOf('(', ftnIndex) + 1;
}
else
{
do
{
openFunctionLine++;
prevLine = capture.GetLineFromLineNumber(openFunctionLine).GetText();
} while (!prevLine.Contains("("));
firstParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start
+ prevLine.IndexOf('(')
+ 1;
}
int lastParenPosition = -1;
if (prevLine.IndexOf(')') > 0)
{
lastParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start
+ prevLine.IndexOf(')', prevLine.IndexOf('('));
}
else
{
do
{
openFunctionLine++;
prevLine = capture.GetLineFromLineNumber(openFunctionLine).GetText();
} while (!prevLine.Contains(")"));
lastParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start +
prevLine.IndexOf(")");
}
return StubUtils
.RemoveComments(capture
.GetText()
.Substring(firstParenPosition, (lastParenPosition - firstParenPosition)))
.Split(',')
.Select(param => param.Trim())
.ToArray();
}
示例4: GetIndention
/// <summary>
/// Gets the number of tabs from the beginning of the line.
/// </summary>
/// <param name="lastSlashPosition"></param>
/// <param name="capture">The snapshot to use as the context of the line.</param>
/// <returns></returns>
public static string GetIndention(int lastSlashPosition, ITextSnapshot capture, bool isAboveFunction = false)
{
int lineNum = capture.GetLineNumberFromPosition(lastSlashPosition);
if (isAboveFunction) { lineNum++; }
else { lineNum--; }
lineNum = GetFunctionDeclarationLineNumber(capture, lineNum, isAboveFunction);
string space = capture.GetLineFromLineNumber(lineNum).GetText();
int leadingSpace = space.Length - space.TrimStart().Length;
space = space.Substring(0, leadingSpace);
if (isAboveFunction) { return space; }
return space + GetTab();
}
示例5: ShouldCreateReturnTag
public static bool ShouldCreateReturnTag(int position, ITextSnapshot capture, bool isAboveFunction = false)
{
if (Options.ReturnGenerationOption == ReturnTagGenerationSetting.Always) return true;
if (Options.ReturnGenerationOption == ReturnTagGenerationSetting.Never) return false;
bool hasReturn = false;
bool newFunction = false;
bool functionClosed = false;
bool hasComment = false;
int lineNumber = capture.GetLineNumberFromPosition(position - 1);
string lineText = capture.GetLineFromLineNumber(lineNumber).GetText();
if (isAboveFunction) { lineNumber++; }
else { lineNumber--; }
bool inFunction = GetFunctionDeclarationLineNumber(capture, lineNumber, isAboveFunction) >= 0;
if (!inFunction) return false;
if (isAboveFunction) { lineNumber = GetNextOpenCurlyBrace(lineNumber, capture); }
if (lineNumber == -1) { return false; }
int functionsOpen = 0;
int openBracket = 0;
for (int i = lineNumber; i < capture.LineCount; i++)
{
lineText = capture.GetLineFromLineNumber(i).GetText();
//HANDLE COMMENTS
if (lineText.Contains("/*") && lineText.Contains("*/") && lineText.LastIndexOf("/*") > lineText.LastIndexOf("*/"))
{
hasComment = true;
}
else if (lineText.Contains("/*") && lineText.Contains("*/"))
{
hasComment = false;
}
else if (lineText.Contains("/*"))
{
hasComment = true;
}
if (hasComment && lineText.Contains("*/"))
{
if (!lineText.Contains("/*") || lineText.LastIndexOf("/*") <= lineText.LastIndexOf("*/"))
hasComment = false;
}
else if (hasComment || String.IsNullOrEmpty(lineText.Trim())) { continue; }
lineText = RemoveComments(lineText);
//END COMMENT HANDLING
//HANDLE BRACKETS - "{ }"
if (javaScriptFnRegex.IsMatch(lineText) && lineText.Contains("{"))
{
//adds an open function and an open bracket.
functionsOpen++;
}
else if (javaScriptFnRegex.IsMatch(lineText))
{
//states that there is a new function open without an open bracket.
newFunction = true;
}
else if (newFunction && lineText.Contains("{"))
{
//states that there is no longer a new function and adds an open
//bracket and open function.
newFunction = false;
functionsOpen++;
}
if (lineText.Contains("{"))
{
//Adds an open bracket.
openBracket++;
}
bool isInlineFunction = false;
if (lineText.Contains("}"))
{
//If function is closed on same line as closing bracket
if (functionsOpen == 1 && returnRegex.IsMatch(lineText))
{
hasReturn = true;
break;
}
else if (returnRegex.IsMatch(lineText))
{
isInlineFunction = true;
}
//Decrements both the number of open brackets and functions if they are equal.
//This means the number of open brackets are the same as the number of open functions.
//Otherwise it just decrements the number of open brackets.
if (openBracket == functionsOpen)
{
functionsOpen--;
}
//.........这里部分代码省略.........
示例6: BuildRegions
private static void BuildRegions(OutlineRegionCollection newRegions, ITextSnapshot snapshot) {
// Figure out regions based on line indent
if (snapshot.LineCount == 0)
return;
var regionStack = new Stack<CodeBlock>();
var lineLengths = new int[snapshot.LineCount];
int lastBlockIndent = 0;
for (int i = 0; i < snapshot.LineCount; i++) {
var line = snapshot.GetLineFromLineNumber(i);
var lineText = line.GetText();
if (String.IsNullOrWhiteSpace(lineText)) {
lineLengths[i] = 0;
continue;
}
lineLengths[i] = line.Length;
int indent = GetLineIndent(line);
if (regionStack.Count > 0)
lastBlockIndent = regionStack.Peek().Indent;
else
lastBlockIndent = 0;
if (indent <= lastBlockIndent) {
// We add regions optimistically since any line can
// start a new region if lines below it are indented
// deeper that this line.
while (regionStack.Count > 0) {
// If we have line with the same indent, remove previously added region
// and replace it with a new one potentially starting with the current line.
var prevCodeBlock = regionStack.Pop();
int startLine = snapshot.GetLineNumberFromPosition(prevCodeBlock.Start);
// Trim empty lines
int j = i - 1;
for (; j >= 0; j--) {
if (lineLengths[j] > 0)
break;
}
j++;
if (j > 0 && j - startLine >= _minLinesToOutline) {
var prevLine = snapshot.GetLineFromLineNumber(j - 1);
if (prevCodeBlock.Start < prevLine.End)
newRegions.Add(OutlineRegion.FromBounds(snapshot.TextBuffer, prevCodeBlock.Start, prevLine.End));
}
if (regionStack.Count > 0) {
prevCodeBlock = regionStack.Peek();
if (prevCodeBlock.Indent < indent)
break;
}
}
}
lastBlockIndent = indent;
regionStack.Push(new CodeBlock(line.Start, indent));
}
// Note that last region may be bogus since we add regions optimistically.
// Remove last region if its indent is the same as the line before it
if (regionStack.Count > 0) {
var codeBlock = regionStack.Peek();
var lineNumber = snapshot.GetLineNumberFromPosition(codeBlock.Start);
if (lineNumber > 0) {
var prevLine = snapshot.GetLineFromLineNumber(lineNumber - 1);
int indent = GetLineIndent(prevLine);
if (indent == codeBlock.Indent)
regionStack.Pop();
}
}
while (regionStack.Count > 0) {
var codeBlock = regionStack.Pop();
int startLine = snapshot.GetLineNumberFromPosition(codeBlock.Start);
if (snapshot.LineCount - startLine >= _minLinesToOutline) {
newRegions.Add(OutlineRegion.FromBounds(snapshot.TextBuffer, codeBlock.Start, snapshot.Length));
}
}
}
示例7: OutlineRange
/// <summary>
/// Determines if range is large enough to be outlined
/// </summary>
private static bool OutlineRange(ITextSnapshot snapshot, ITextRange range, out int startLineNumber, out int endLineNumber) {
int start = Math.Max(0, range.Start);
int end = Math.Min(range.End, snapshot.Length);
startLineNumber = endLineNumber = 0;
if (start < end) {
startLineNumber = snapshot.GetLineNumberFromPosition(start);
endLineNumber = snapshot.GetLineNumberFromPosition(end);
return endLineNumber - startLineNumber + 1 >= _minLinesToOutline;
}
return false;
}
示例8: FindLineBeforeWhichToInsertMethodStatement
private int FindLineBeforeWhichToInsertMethodStatement(ITextSnapshot snapshot, int p)
{
CodeNavigator navigator = new CodeNavigator(snapshot);
int lastLineOfMethod = -1;
foreach (int significantPoint in navigator.DownFrom(p))
{
lastLineOfMethod = significantPoint;
}
if (lastLineOfMethod > 0) return snapshot.GetLineNumberFromPosition(lastLineOfMethod);
return -1;
}
示例9: ReportParseErrors
public void ReportParseErrors(IParseResult parseResult, ITextSnapshot snapshot)
{
_errorListProvider.SuspendRefresh();
try
{
// remove any previously created errors to get a clean start
ClearErrors();
var messages = (CompilerMessageList)parseResult.CompilerMessages;
foreach (var error in messages.GetMessages())
{
// creates the instance that will be added to the Error List
var nSpan = error.Location.Span;
var span = new Span(nSpan.StartPos, nSpan.Length);
if (span.Start >= snapshot.Length)
continue;
ErrorTask task = new ErrorTask();
task.Category = TaskCategory.All;
task.Priority = TaskPriority.Normal;
task.Document = _textBuffer.Properties.GetProperty<ITextDocument>(typeof(ITextDocument)).FilePath;
task.ErrorCategory = TranslateErrorCategory(error);
task.Text = error.Text;
task.Line = snapshot.GetLineNumberFromPosition(span.Start);
task.Column = span.Start - snapshot.GetLineFromLineNumber(task.Line).Start;
task.Navigate += OnTaskNavigate;
_errorListProvider.Tasks.Add(task);
_previousErrors.Add(task);
var trackingSpan = snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeNegative);
_squiggleTagger.CreateTagSpan(trackingSpan, new ErrorTag("syntax error", error.Text));
_previousSquiggles.Add(new TrackingTagSpan<IErrorTag>(trackingSpan, new ErrorTag("syntax error", error.Text)));
}
}
finally { _errorListProvider.ResumeRefresh(); }
}
示例10: FindLineAfterWhichToInsertDeclaration
private int FindLineAfterWhichToInsertDeclaration(ITextSnapshot snapshot, int p)
{
CodeNavigator navigator = new CodeNavigator(snapshot);
IEnumerator<int> point = navigator.UpFrom(p).GetEnumerator();
int originalLine = snapshot.GetLineNumberFromPosition(p);
int lineNumber = -1;
bool notEOF;
do
{
notEOF = point.MoveNext();
lineNumber = snapshot.GetLineNumberFromPosition(point.Current);
}
while( lineNumber == originalLine && notEOF) ;
if (lineNumber == originalLine) lineNumber = -1;
return lineNumber;
}
示例11: FindLineAfterWhichToInsertFieldDeclaration
private int FindLineAfterWhichToInsertFieldDeclaration(ITextSnapshot snapshot, int p)
{
CodeNavigator navigator = new CodeNavigator(snapshot);
foreach (int significantPoint in navigator.UpFrom(p))
{
if (navigator.IsInClassScope(significantPoint)) return snapshot.GetLineNumberFromPosition(significantPoint);
}
return -1;
}
示例12: IsMultilineToken
protected virtual bool IsMultilineToken(ITextSnapshot snapshot, ITokenSource lexer, IToken token)
{
Lexer lexerLexer = lexer as Lexer;
if (lexerLexer != null && lexerLexer.Line >= token.Line)
return false;
int startLine = snapshot.GetLineNumberFromPosition(token.StartIndex);
int stopLine = snapshot.GetLineNumberFromPosition(token.StopIndex + 1);
return startLine != stopLine;
}
示例13: TokensSkippedLines
private static bool TokensSkippedLines(ITextSnapshot snapshot, int endLinePrevious, IToken token)
{
int startLineCurrent = snapshot.GetLineNumberFromPosition(token.StartIndex);
return startLineCurrent > endLinePrevious + 1;
}
示例14: ParseMarkdownSections
/// <summary>
/// Parse a text snapshot into markdown sections. This is only part of the markdown parser that is really aware of the editor,
/// but it keeps us from re-creating all the "GetLineFromLineNumber"-type methods.
/// </summary>
/// <param name="snapshot"></param>
/// <returns></returns>
public static IEnumerable<Token> ParseMarkdownSections(ITextSnapshot snapshot)
{
string text = snapshot.GetText();
List<Tuple<int, TokenType>> startPoints =
new List<Tuple<int, TokenType>>(ParseMarkdownParagraph(text).Where(t => IsHeaderToken(t))
.Select(t => Tuple.Create(t.Span.Start, t.TokenType)));
List<Token> sections = new List<Token>();
Stack<Tuple<int, TokenType>> regions = new Stack<Tuple<int, TokenType>>();
foreach (var start in startPoints)
{
int previousLineNumber = Math.Max(0, snapshot.GetLineNumberFromPosition(start.Item1) - 1);
int end = snapshot.GetLineFromLineNumber(previousLineNumber).End;
while (regions.Count > 0 && regions.Peek().Item2 >= start.Item2)
{
var region = regions.Pop();
var span = Span.FromBounds(region.Item1, end);
sections.Add(new Token(region.Item2, span));
}
regions.Push(start);
}
while (regions.Count > 0)
{
var region = regions.Pop();
var span = Span.FromBounds(region.Item1, snapshot.Length);
sections.Add(new Token(region.Item2, span));
}
sections.Sort((left, right) =>
{
if (left.Span.Start != right.Span.Start)
return left.Span.Start.CompareTo(right.Span.Start);
return right.Span.Length.CompareTo(left.Span.Length);
});
return sections;
}
示例15: ApplyChange
/// <summary>
/// Rescans the part of the buffer affected by a change.
/// Scans a contiguous sub-<paramref name="span"/> of a larger code span which starts at <paramref name="codeStartLine"/>.
/// </summary>
private void ApplyChange(Tokenizer tokenizer, ITextSnapshot snapshot, Span span)
{
int firstLine = snapshot.GetLineNumberFromPosition(span.Start);
int lastLine = snapshot.GetLineNumberFromPosition(span.Length > 0 ? span.End - 1 : span.End);
Contract.Assert(firstLine >= 0);
// find the closest line preceding firstLine for which we know categorizer state, stop at the codeStartLine:
LineTokenization lineTokenization;
firstLine = _tokenCache.IndexOfPreviousTokenization(firstLine, 0, out lineTokenization) + 1;
object state = lineTokenization.State;
int currentLine = firstLine;
object previousState;
while (currentLine < snapshot.LineCount) {
previousState = _tokenCache.TryGetTokenization(currentLine, out lineTokenization) ? lineTokenization.State : null;
_tokenCache[currentLine] = lineTokenization = TokenizeLine(tokenizer, snapshot, state, currentLine);
state = lineTokenization.State;
// stop if we visted all affected lines and the current line has no tokenization state or its previous state is the same as the new state:
if (currentLine > lastLine && (previousState == null || previousState.Equals(state))) {
break;
}
currentLine++;
}
// classification spans might have changed between the start of the first and end of the last visited line:
int changeStart = snapshot.GetLineFromLineNumber(firstLine).Start;
int changeEnd = (currentLine < snapshot.LineCount) ? snapshot.GetLineFromLineNumber(currentLine).End : snapshot.Length;
if (changeStart < changeEnd) {
var classificationChanged = ClassificationChanged;
if (classificationChanged != null) {
var args = new ClassificationChangedEventArgs(new SnapshotSpan(snapshot, new Span(changeStart, changeEnd - changeStart)));
classificationChanged(this, args);
}
}
}