本文整理汇总了C#中Stack.PeekOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.PeekOrDefault方法的具体用法?C# Stack.PeekOrDefault怎么用?C# Stack.PeekOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.PeekOrDefault方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyToRange
static void ApplyToRange(ITextEditor editor, Stack<string> indentation, int begin, int end, int selBegin, int selEnd)
{
bool multiLine = false;
for (int i = begin; i <= end; i++) {
IDocumentLine curLine = editor.Document.GetLine(i);
string lineText = curLine.Text.TrimStart(' ', '\t', '\r', '\n');
string noComments = lineText.TrimComments().TrimEnd(' ', '\t', '\r', '\n');
if (i < selBegin || i > selEnd) {
indentation.PopOrDefault();
indentation.Push(DocumentUtilitites.GetWhitespaceAfter(editor.Document, curLine.Offset));
}
// change indentation before (indent this line)
if (multiLine && noComments.EndsWith("}", StringComparison.OrdinalIgnoreCase)) {
Unindent(indentation);
multiLine = false;
}
editor.Document.SmartReplaceLine(curLine, (indentation.PeekOrDefault() ?? string.Empty) + lineText);
// change indentation afterwards (indent next line)
if (!multiLine && noComments.EndsWith("_", StringComparison.OrdinalIgnoreCase)) {
Indent(editor, indentation);
multiLine = true;
}
if (multiLine && !noComments.EndsWith("_", StringComparison.OrdinalIgnoreCase)) {
multiLine = false;
Unindent(indentation);
}
}
}
示例2: Indent
static void Indent(ITextEditor editor, Stack<string> indentation)
{
indentation.Push((indentation.PeekOrDefault() ?? string.Empty) + editor.Options.IndentationString);
}
示例3: ApplyToRange
static void ApplyToRange(ITextEditor editor, Stack<string> indentation, List<int> eols, int blockStart, int blockEnd, int selectionStart, int selectionEnd, bool sawAttribute) {
LoggingService.InfoFormatted("indenting line {0} to {1} with {2}", blockStart, blockEnd, (indentation.PeekOrDefault() ?? "").Length);
int nextEol = -1;
bool wasMultiLine = false;
for (int i = blockStart; i <= blockEnd; i++) {
IDocumentLine curLine = editor.Document.GetLine(i);
string lineText = curLine.Text.TrimStart();
// preprocessor directives cannot be multiline (just as comments)
// and they are not included in normal block indentation ->
// treat preprocessor directives as comments -> remove them
string noComments = lineText.TrimComments().TrimPreprocessorDirectives().TrimEnd().TrimEnd('_').TrimEnd();
// adjust indentation if the current line is not selected
// lines between the selection will be aligned to the selected level
if (i < selectionStart || i > selectionEnd) {
indentation.PopOrDefault();
indentation.Push(DocumentUtilitites.GetWhitespaceAfter(editor.Document, curLine.Offset));
}
// look for next eol if line is not empty
// (the lexer does not produce eols for empty lines)
if (!string.IsNullOrEmpty(noComments) && i >= nextEol) {
int search = eols.BinarySearch(i);
if (search < 0)
search = ~search;
nextEol = search < eols.Count ? eols[search] : i;
}
// remove indentation in last line of multiline array(, collection, object) initializers
if (i == nextEol && wasMultiLine && (noComments == "}" || sawAttribute)) {
wasMultiLine = false;
Unindent(indentation);
}
// apply the indentation
editor.Document.SmartReplaceLine(curLine, (indentation.PeekOrDefault() ?? "") + lineText);
// indent line if it is ended by (implicit) line continuation
if (i < nextEol && !wasMultiLine) {
wasMultiLine = true;
Indent(editor, indentation);
}
// unindent if this is the last line of a multiline statement
if (i == nextEol && wasMultiLine) {
wasMultiLine = false;
Unindent(indentation);
}
}
}
示例4: SmartIndentInternal
static int SmartIndentInternal(ITextEditor editor, int begin, int end)
{
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text));
Stack<string> indentation = new Stack<string>();
indentation.Push(string.Empty);
int oldLine = 1;
bool inInterface = false;
bool isMustOverride = false;
bool isDeclare = false;
bool isDelegate = false;
Token currentToken = null;
Token prevToken = null;
while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) {
if (prevToken == null)
prevToken = currentToken;
if (currentToken.Kind == Tokens.MustOverride)
isMustOverride = true;
if (currentToken.Kind == Tokens.Delegate)
isDelegate = true;
if (currentToken.Kind == Tokens.Declare)
isDeclare = true;
if (currentToken.Kind == Tokens.EOL)
isDelegate = isDeclare = isMustOverride = false;
if (IsBlockEnd(currentToken, prevToken)) {
ApplyToRange(editor, indentation, oldLine, currentToken.Location.Line, begin, end);
if (currentToken.Kind == Tokens.Interface)
inInterface = false;
if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) {
Unindent(indentation);
if (currentToken.Kind == Tokens.Select)
Unindent(indentation);
}
oldLine = currentToken.Location.Line;
}
if (IsBlockStart(lexer, currentToken, prevToken)) {
int line = GetLastVisualLine(currentToken.Location.Line, editor);
ApplyToRange(editor, indentation, oldLine, line, begin, end);
if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) {
Indent(editor, indentation);
if (currentToken.Kind == Tokens.Select)
Indent(editor, indentation);
}
if (currentToken.Kind == Tokens.Interface)
inInterface = true;
oldLine = line + 1;
}
prevToken = currentToken;
}
// do last indent step
int newLine = prevToken.Location.Line;
if (oldLine > newLine)
newLine = oldLine;
ApplyToRange(editor, indentation, oldLine, newLine, begin, end);
return (indentation.PeekOrDefault() ?? string.Empty).Length;
}
示例5: SmartIndentInternal
//.........这里部分代码省略.........
while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) {
if (context.InContext(Context.Attribute) && currentToken.Kind == Tokens.GreaterThan)
sawAttribute = true;
context.InformToken(currentToken);
if (prevToken == null)
prevToken = currentToken;
if (currentToken.Kind == Tokens.MustOverride)
isMustOverride = true;
if (currentToken.Kind == Tokens.Delegate)
isDelegate = true;
if (currentToken.Kind == Tokens.Declare)
isDeclare = true;
if (currentToken.Kind == Tokens.EOL) {
isDelegate = isDeclare = isMustOverride = sawAttribute = false;
eols.Add(currentToken.Location.Line);
}
if (IsBlockEnd(currentToken, prevToken)) {
// indent the lines inside the block
// this is an End-statement
// hence we indent from blockStart to the previous line
int blockEnd = currentToken.Location.Line - 1;
// if this is a lambda end include End-Statement in block
// if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) {
// blockEnd++;
// }
ApplyToRange(editor, indentation, eols, blockStart, blockEnd, begin, end, sawAttribute);
if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) {
Unindent(indentation);
ApplyToRange(editor, indentation, eols, currentToken.Location.Line, currentToken.Location.Line, begin, end, sawAttribute);
}
if (currentToken.Kind == Tokens.Interface)
inInterface = false;
if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) {
Unindent(indentation);
if (currentToken.Kind == Tokens.Select)
Unindent(indentation);
}
// block start is this line (for the lines between two blocks)
blockStart = currentToken.Location.Line;
if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) {
blockStart++;
lambdaNesting--;
}
}
bool isMultiLineLambda;
if (IsBlockStart(lexer, currentToken, prevToken, out isMultiLineLambda)) {
// indent the lines between the last and this block
// this is a Begin-statement
// hence we indent from blockStart to the this line
int lastVisualLine = FindNextEol(lexer);
eols.Add(lastVisualLine);
ApplyToRange(editor, indentation, eols, blockStart, lastVisualLine, begin, end, sawAttribute);
if (isMultiLineLambda && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) {
lambdaNesting++;
int endColumn = currentToken.Location.Column;
int startColumn = DocumentUtilitites.GetWhitespaceAfter(editor.Document, editor.Document.GetLine(lastVisualLine).Offset).Length;
if (startColumn < endColumn)
Indent(editor, indentation, new string(' ', endColumn - startColumn - 1));
}
if (!inInterface && !isMustOverride && !isDeclare && !isDelegate && !IsAutomaticPropertyWithDefaultValue(lexer, currentToken, prevToken)) {
Indent(editor, indentation);
if (currentToken.Kind == Tokens.Select)
Indent(editor, indentation);
}
if (currentToken.Kind == Tokens.Interface)
inInterface = true;
// block start is the following line (for the lines inside a block)
blockStart = lastVisualLine + 1;
}
prevToken = currentToken;
}
ApplyToRange(editor, indentation, eols, blockStart, editor.Document.TotalNumberOfLines, begin, end, sawAttribute);
return (indentation.PeekOrDefault() ?? string.Empty).Length;
}