public override void WriteToken(Role role, string token, TextTokenType tokenType)
		{
			WriteIndentation();
			column += token.Length;
			textWriter.Write(token);
			isAtStartOfLine = false;
		}
		public override void WriteToken(Role role, string token, TextTokenType tokenType)
		{
			// Avoid that two +, - or ? tokens are combined into a ++, -- or ?? token.
			// Note that we don't need to handle tokens like = because there's no valid
			// C# program that contains the single token twice in a row.
			// (for +, - and &, this can happen with unary operators;
			// for ?, this can happen in "a is int? ? b : c" or "a as int? ?? 0";
			// and for /, this can happen with "1/ *ptr" or "1/ //comment".)
			if (lastWritten == LastWritten.Plus && token[0] == '+' ||
			    lastWritten == LastWritten.Minus && token[0] == '-' ||
			    lastWritten == LastWritten.Ampersand && token[0] == '&' ||
			    lastWritten == LastWritten.QuestionMark && token[0] == '?' ||
			    lastWritten == LastWritten.Division && token[0] == '*') {
				base.Space();
			}
			base.WriteToken(role, token, tokenType);
			if (token == "+") {
				lastWritten = LastWritten.Plus;
			} else if (token == "-") {
				lastWritten = LastWritten.Minus;
			} else if (token == "&") {
				lastWritten = LastWritten.Ampersand;
			} else if (token == "?") {
				lastWritten = LastWritten.QuestionMark;
			} else if (token == "/") {
				lastWritten = LastWritten.Division;
			} else {
				lastWritten = LastWritten.Other;
			}
		}
		public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType)
		{
			WriteIndentation();
			if (tokenType != TextTokenType.Keyword && (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier))) {
				textWriter.Write('@');
				column++;
			}
			textWriter.Write(identifier.Name);
			column += identifier.Name.Length;
			isAtStartOfLine = false;
		}
		public override void WriteToken(Role role, string token, TextTokenType tokenType)
		{
			CSharpTokenNode t = new CSharpTokenNode(locationProvider.Location, (TokenRole)role);
			t.Role = role;
			EmptyStatement node = nodes.Peek().LastOrDefault() as EmptyStatement;
			if (node == null)
				currentList.Add(t);
			else {
				node.Location = locationProvider.Location;
			}
			base.WriteToken(role, token, tokenType);
		}
		public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType)
		{
			if (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier)) {
				if (lastWritten == LastWritten.KeywordOrIdentifier) {
					// this space is not strictly required, so we call Space()
					Space();
				}
			} else if (lastWritten == LastWritten.KeywordOrIdentifier) {
				// this space is strictly required, so we directly call the formatter
				base.Space();
			}
			base.WriteIdentifier(identifier, tokenType);
			lastWritten = LastWritten.KeywordOrIdentifier;
		}
Example #6
0
		static void UpdateTextEditorResource(TextTokenType colorType, string name) {
			var theme = Themes.Theme;

			var color = theme.GetColor(colorType).TextInheritedColor;
			App.Current.Resources[Theme.GetTextInheritedForegroundResourceKey(name)] = GetBrush(color.Foreground);
			App.Current.Resources[Theme.GetTextInheritedBackgroundResourceKey(name)] = GetBrush(color.Background);
			App.Current.Resources[Theme.GetTextInheritedFontStyleResourceKey(name)] = color.FontStyle ?? FontStyles.Normal;
			App.Current.Resources[Theme.GetTextInheritedFontWeightResourceKey(name)] = color.FontWeight ?? FontWeights.Normal;

			color = theme.GetColor(colorType).InheritedColor;
			App.Current.Resources[Theme.GetInheritedForegroundResourceKey(name)] = GetBrush(color.Foreground);
			App.Current.Resources[Theme.GetInheritedBackgroundResourceKey(name)] = GetBrush(color.Background);
			App.Current.Resources[Theme.GetInheritedFontStyleResourceKey(name)] = color.FontStyle ?? FontStyles.Normal;
			App.Current.Resources[Theme.GetInheritedFontWeightResourceKey(name)] = color.FontWeight ?? FontWeights.Normal;
		}
Example #7
0
		public bool Find(int offset, out int defaultTextLength, out TextTokenType tokenType, out int tokenLength) {
			Debug.Assert(tokenInfos != null, "You must call Finish() before you call this method");
			int index;
			if (!offsetToTokenInfoIndex.TryGetValue(offset, out index)) {
				defaultTextLength = 0;
				tokenType = TextTokenType.Last;
				tokenLength = 0;
				return false;
			}

			ushort val = tokenInfos[index];
			defaultTextLength = (val >> TEXT_TOKEN_LENGTH_BIT) & TEXT_TOKEN_LENGTH_MAX;
			tokenType = (TextTokenType)((val >> TOKEN_TYPE_BIT) & TOKEN_TYPE_MAX);
			tokenLength = (val >> TOKEN_LENGTH_BIT) & TOKEN_LENGTH_MAX;
			return true;
		}
Example #8
0
		public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType)
		{
			if (tokenType == TextTokenType.Text)
				tokenType = TextTokenHelper.GetTextTokenType(identifier.AnnotationVT<TextTokenType>() ?? identifier.Annotation<object>());

			if (tokenType != TextTokenType.Keyword && (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier))) {
				output.Write('@', TextTokenType.Operator);
			}
			
			var definition = GetCurrentDefinition(identifier);
			if (definition != null) {
				output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType, false);
				return;
			}
			
			object memberRef = GetCurrentMemberReference();

			if (memberRef != null) {
				output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType);
				return;
			}

			definition = GetCurrentLocalDefinition();
			if (definition != null) {
				output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType);
				return;
			}

			memberRef = GetCurrentLocalReference();
			if (memberRef != null) {
				output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType, true);
				return;
			}

			if (firstUsingDeclaration) {
				output.MarkFoldStart(defaultCollapsed: true);
				firstUsingDeclaration = false;
			}

			var s = identifier.Name;
			if (identifier.Annotation<IdentifierFormatted>() == null)
				s = IdentifierEscaper.Escape(s);
			output.Write(s, tokenType);
		}
Example #9
0
		public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType)
		{
			if (tokenType == TextTokenType.Text)
				tokenType = TextTokenHelper.GetTextTokenType(identifier.AnnotationVT<TextTokenType>() ?? identifier.Annotation<object>());

			var definition = GetCurrentDefinition(identifier);
			if (definition != null) {
				output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType, false);
				return;
			}
			
			object memberRef = GetCurrentMemberReference();

			if (memberRef != null) {
				output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType);
				return;
			}

			definition = GetCurrentLocalDefinition();
			if (definition != null) {
				output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType);
				return;
			}

			memberRef = GetCurrentLocalReference();
			if (memberRef != null) {
				output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType, true);
				return;
			}

			if (firstUsingDeclaration) {
				output.MarkFoldStart(defaultCollapsed: true);
				firstUsingDeclaration = false;
			}

			output.Write(IdentifierEscaper.Escape(identifier.Name), tokenType);
		}
Example #10
0
 public void Write(string text, TextTokenType tokenType)
 {
     WriteIndent();
     Append(tokenType, text);
 }
Example #11
0
 public void Write(char ch, TextTokenType tokenType)
 {
     WriteIndent();
     Append(tokenType, ch);
 }
Example #12
0
 void Append(TextTokenType tokenType, char c)
 {
     tokens.Append(tokenType, c);
     b.Append(c);
     Debug.Assert(b.Length == tokens.Length);
 }
        public IEnumerator <string> GetEnumerator()
        {
            bool          insideQuote = false;
            TextTokenType quoteType   = TextTokenType.Separator;
            StringBuilder result      = new StringBuilder();

            foreach (TextToken token in tokenizer)
            {
                switch (token.Type)
                {
                case TextTokenType.Separator:
                    if (insideQuote)
                    {
                        result.Append(token.Value);
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(token.Value) || result.Length > 0)
                        {
                            yield return(result.ToString());
                        }
                        result.Length = 0;
                    }
                    break;

                case TextTokenType.SingleQuote:
                case TextTokenType.DoubleQoute:
                    if (!insideQuote)
                    {
                        quoteType   = token.Type;
                        insideQuote = true;
                        if (result.Length > 0)
                        {
                            yield return(result.ToString());
                        }
                        result.Length = 0;
                    }
                    else if (token.Type == quoteType)
                    {
                        insideQuote = false;
                        quoteType   = TextTokenType.Separator;
                    }
                    else
                    {
                        result.Append(token.Value);
                    }
                    break;

                case TextTokenType.Value:
                    result.Append(token.Value);
                    break;

                default:
                    throw new InvalidOperationException("Unknown token type: " + token.Type);
                }
            }
            if (result.Length > 0)
            {
                yield return(result.ToString());
            }
        }
Example #14
0
        // Gets called to add one token. No newlines are allowed
        void AppendInternal(TextTokenType tokenType, int length)
        {
            Debug.Assert(length >= 0);
            if (length == 0)
                return;

            redo:
            if (isAppendingDefaultText) {
                if (tokenType == TextTokenType.Text) {
                    int newLength = currentDefaultTextLength + length;
                    while (newLength > TEXT_TOKEN_LENGTH_MAX) {
                        currentDefaultTextLength = Math.Min(newLength, TEXT_TOKEN_LENGTH_MAX);
                        EndCurrentToken(0);
                        newLength -= TEXT_TOKEN_LENGTH_MAX;
                    }
                    currentDefaultTextLength = newLength;
                    currentOffset += length;
                    return;
                }
                isAppendingDefaultText = false;
                currentTokenType = tokenType;
            }

            if (currentTokenType != tokenType) {
                EndCurrentToken(0);
                goto redo;
            }

            {
                int newLength = currentTokenLength + length;
                while (newLength > TOKEN_LENGTH_MAX) {
                    currentTokenLength = Math.Min(newLength, TOKEN_LENGTH_MAX);
                    EndCurrentToken(0);
                    newLength -= TOKEN_LENGTH_MAX;
                    isAppendingDefaultText = false;
                    currentTokenType = tokenType;
                }
                currentTokenLength = newLength;
                if (currentTokenLength == 0)
                    isAppendingDefaultText = true;
                currentOffset += length;
            }
        }
Example #15
0
 public Color GetColor(TextTokenType tokenType)
 {
     return GetColor((ColorType)tokenType);
 }
Example #16
0
 void Append(TextTokenType tokenType, char c)
 {
     tokens.Append(tokenType, c);
     b.Append(c);
     Debug.Assert(b.Length == tokens.Length);
 }
Example #17
0
 void IXmlDocOutput.Write(string s, TextTokenType tokenType)
 {
     ret.Append(s);
 }
Example #18
0
 public TextToken(TextTokenType type, string text)
 {
     Type = type;
     Text = text;
 }
Example #19
0
 public void Append(TextTokenType tokenType, char c)
 {
     Append(tokenType, c.ToString());
 }
Example #20
0
        public static void WriteOffsetReference(ITextOutput writer, Instruction instruction, MethodDef method, TextTokenType tokenType = TextTokenType.Label)
        {
            var r = instruction == null ? null : method == null ? (object)instruction : new InstructionReference(method, instruction);

            writer.WriteReference(DnlibExtensions.OffsetToString(instruction.GetOffset()), r, tokenType);
        }
Example #21
0
 public SimpleType(TextTokenType tokenType, string identifier, TextLocation location)
 {
     SetChildByRole(Roles.Identifier, new Identifier (tokenType, identifier, location));
 }
 public TextToken(TextTokenType type, string value)
 {
     Value = value;
     Type  = type;
 }
Example #23
0
 public void WriteDefinition(string text, object definition, TextTokenType tokenType, bool isLocal)
 {
     WriteIndent();
     int start = this.TextLength;
     Append(tokenType, text);
     int end = this.TextLength;
     this.DefinitionLookup.AddDefinition(definition, this.TextLength);
     references.Add(new ReferenceSegment { StartOffset = start, EndOffset = end, Reference = definition, IsLocal = isLocal, IsLocalTarget = true });
 }
 public void WriteIdentifier(string ident, TextTokenType tokenType)
 {
     WriteIndentation();
     textWriter.Write(ident);
 }
Example #25
0
 public void WriteReference(string text, object reference, TextTokenType tokenType, bool isLocal)
 {
     WriteIndent();
     int start = this.TextLength;
     Append(tokenType, text);
     int end = this.TextLength;
     references.Add(new ReferenceSegment { StartOffset = start, EndOffset = end, Reference = reference, IsLocal = isLocal });
 }
Example #26
0
 public void Append(TextTokenType tokenType, char c)
 {
     Append(tokenType, c.ToString());
 }
Example #27
0
 void Append(TextTokenType tokenType, string s)
 {
     tokens.Append(tokenType, s);
     b.Append(s);
     Debug.Assert(b.Length == tokens.Length);
 }
		public override void WritePrimitiveValue(object value, TextTokenType? tokenType = null, string literalValue = null)
		{
			Expression node = nodes.Peek().LastOrDefault() as Expression;
			if (node is PrimitiveExpression) {
				((PrimitiveExpression)node).SetStartLocation(locationProvider.Location);
			}
			if (node is NullReferenceExpression) {
				((NullReferenceExpression)node).SetStartLocation(locationProvider.Location);
			}
			base.WritePrimitiveValue(value, tokenType, literalValue);
		}
Example #29
0
        public void Append(TextTokenType tokenType, string s)
        {
            int oldCurrentOffset = currentOffset;

            // Newlines could be part of the input string
            int so = 0;
            while (so < s.Length) {
                int nlOffs = s.IndexOfAny(newLineChars, so);
                if (nlOffs >= 0) {
                    AppendInternal(tokenType, nlOffs - so);
                    so = nlOffs;
                    int nlLen = s[so] == '\r' && so + 1 < s.Length && s[so + 1] == '\n' ? 2 : 1;
                    currentOffset += nlLen;
                    EndCurrentToken(nlLen);
                    so += nlLen;
                }
                else {
                    AppendInternal(tokenType, s.Length - so);
                    break;
                }
            }

            Debug.Assert(oldCurrentOffset + s.Length == currentOffset);
        }
Example #30
0
 public SimpleType(TextTokenType tokenType, string identifier)
 {
     this.IdentifierToken = Ast.Identifier.Create(tokenType, identifier);
 }
Example #31
0
        void EndCurrentToken(int lengthTillNextToken)
        {
            Debug.Assert(currentTokenLength == 0 || !isAppendingDefaultText);
            int totalLength = currentDefaultTextLength + currentTokenLength;
            if (totalLength != 0) {
                offsetToTokenInfoIndex.Add(currentTokenOffset, tokenInfosList.Count);
                Debug.Assert((int)currentTokenType <= TOKEN_TYPE_MAX);
                Debug.Assert((int)currentTokenLength <= TOKEN_LENGTH_MAX);
                Debug.Assert((int)currentDefaultTextLength <= TEXT_TOKEN_LENGTH_MAX);
                ushort compressedValue = (ushort)(
                            ((int)currentTokenType << TOKEN_TYPE_BIT) |
                            (currentTokenLength << TOKEN_LENGTH_BIT) |
                            (currentDefaultTextLength << TEXT_TOKEN_LENGTH_BIT));
                tokenInfosList.Add(compressedValue);
            }

            currentTokenOffset += totalLength + lengthTillNextToken;
            currentDefaultTextLength = 0;
            currentTokenLength = 0;
            currentTokenType = TextTokenType.Last;
            isAppendingDefaultText = true;
        }
Example #32
0
 HighlightingColor GetColor(TextTokenType tokenType)
 {
     return(Themes.Theme.GetColor(tokenType).TextInheritedColor);
 }
		public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType)
		{
			if (!identifier.IsNull)
				identifier.SetStartLocation(locationProvider.Location);
			currentList.Add(identifier);
			base.WriteIdentifier(identifier, tokenType);
		}
Example #34
0
			public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType)
			{
				int startOffset = stringWriter.GetStringBuilder ().Length;
				int endOffset = startOffset + (identifier.Name ?? "").Length + (identifier.IsVerbatim ? 1 : 0);
				NewSegments.Add(new KeyValuePair<AstNode, Segment>(identifier, new Segment(startOffset, endOffset - startOffset)));
				base.WriteIdentifier (identifier, tokenType);
			}
Example #35
0
 HighlightingColor GetColor(TextTokenType tokenType)
 {
     var color = Themes.Theme.GetColor(tokenType).TextInheritedColor;
     Debug.Assert(color != null);
     return color;
 }
Example #36
0
 void Append(TextTokenType tokenType, string s)
 {
     tokens.Append(tokenType, s);
     b.Append(s);
     Debug.Assert(b.Length == tokens.Length);
 }
Example #37
0
 public static void WriteOffsetReference(ITextOutput writer, Instruction instruction, MethodDef method, TextTokenType tokenType = TextTokenType.Label)
 {
     var r = instruction == null ? null : method == null ? (object)instruction : new InstructionReference(method, instruction);
     writer.WriteReference(DnlibExtensions.OffsetToString(instruction.GetOffset()), r, tokenType);
 }
Example #38
0
 public override void WritePrimitiveValue(object value, TextTokenType? tokenType = null, string literalValue = null)
 {
     int column = 0;
     TextWriterTokenWriter.WritePrimitiveValue(value, tokenType, literalValue, ref column, (a, b) => output.Write(a, b), (a, b, c) => WriteToken(a, b, c));
 }
Example #39
0
 public void Write(string s, TextTokenType tokenType)
 {
     if (needsNewLine)
         WriteNewLine();
     output.Write(s, tokenType);
 }
Example #40
0
 public void Write(string text, TextTokenType tokenType)
 {
     WriteIndent();
     Append(tokenType, text);
 }
Example #41
0
 void IXmlDocOutput.Write(string s, TextTokenType tokenType)
 {
     ret.Append(s);
 }
Example #42
0
 public void WriteToken(string token, TextTokenType tokenType)
 {
     // Attach member reference to token only if there's no identifier in the current node.
     IMemberRef memberRef = GetCurrentMemberReference();
     if (memberRef != null && nodeStack.Peek().GetChildByRole(AstNode.Roles.Identifier).IsNull)
         output.WriteReference(token, memberRef, tokenType);
     else
         output.Write(token, tokenType);
 }
Example #43
0
        public override void WriteToken(Role role, string token, TextTokenType tokenType)
        {
            IMemberRef memberRef = GetCurrentMemberReference();
            var node = nodeStack.Peek();

            bool addRef = memberRef != null &&
                    (node is BinaryOperatorExpression ||
                    node is UnaryOperatorExpression ||
                    node is AssignmentExpression ||
                    node is IndexerExpression);

            // Add a ref to the method if it's a delegate call
            if (!addRef && node is InvocationExpression && memberRef is IMethod) {
                var md = (memberRef as IMethod).Resolve();
                if (md != null && md.DeclaringType.IsDelegate())
                    addRef = true;
            }

            if (addRef)
                output.WriteReference(token, memberRef, tokenType);
            else
                output.Write(token, tokenType);
        }
Example #44
0
 public void Write(char ch, TextTokenType tokenType)
 {
     WriteIndent();
     Append(tokenType, ch);
 }