예제 #1
0
파일: Token.cs 프로젝트: Stmated/texteditor
 public Token(ITextView textView, TokenTypeBase tokenType, string foundAs)
 {
     this.TextView      = textView;
     this.Type          = tokenType;
     this.ItemSeparator = Strings.Native_ItemSeparator;
     this.FoundAs       = foundAs;
 }
예제 #2
0
        private IEnumerable <Token> GetTokens(ITextView textView)
        {
            var matches       = Regex.Matches(this.ContentRaw, PLACEHOLDER_PATTERN, RegexOptions.IgnoreCase);
            var nextMatchIdx  = 0;
            var tokenFreetext = new TokenTypes.TokenTypeFreetext();
            var previousIdx   = 0;

            for (var i = 0; i < this.ContentRaw.Length; i++)
            {
                if (nextMatchIdx == matches.Count)
                {
                    var t = new Token(textView, tokenFreetext, null);
                    t.Attributes = new[] { new TokenAttribute(t, new AttribTypeContent(), this.ContentRaw.Substring(i)) };

                    yield return(t);

                    break;
                }

                var nextMatch = matches[nextMatchIdx];

                if (nextMatch.Index != i)
                {
                    continue;
                }

                var freeText      = this.ContentRaw.Substring(previousIdx, i - previousIdx);
                var freetextToken = new Token(textView, tokenFreetext, null);
                freetextToken.Attributes = new[] { new TokenAttribute(freetextToken, new AttribTypeContent(), freeText) };
                yield return(freetextToken);

                var           tokenTypeKey    = nextMatch.Groups[1].Value;
                var           attributeString = nextMatch.Groups[2].Value;
                var           attributes      = new List <TokenAttribute>();
                TokenTypeBase foundTokenType  = null;

                foreach (var ttt in textView.Settings.GetTemplateTokenTypes().Where(ttt => ttt.Key == tokenTypeKey && !ttt.IsDynamic))
                {
                    foundTokenType = ttt;
                }

                if (foundTokenType == null)
                {
                    #region Get dynamic variable

                    var dynamicTokens = textView.Settings.GetTemplateTokenTypes().Where(ttt => ttt.IsDynamic).ToList();

                    if (dynamicTokens.Count > 1)
                    {
                        // TODO: Something should be done here to ask the user which method (s)he wants to use.
                        //       But this should only happen if a user has added another through plugins, so no rush to implement.
                    }

                    if (dynamicTokens.Count == 0)
                    {
                        textView.Settings.Notifier.Error(Strings.TextControl_Title, Strings.Text_Template_DynamicTokenNotFound);
                    }
                    else
                    {
                        foundTokenType = dynamicTokens[0];
                    }

                    #endregion
                }

                var token = new Token(textView, foundTokenType, tokenTypeKey);

                foreach (var attributeKeyValue in attributeString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var    keyValue = attributeKeyValue.Split('=');
                    string key;
                    string value;

                    switch (keyValue.Length)
                    {
                    case 2:
                        key   = keyValue[0];
                        value = keyValue[1];
                        break;

                    case 1:
                        key   = attributeKeyValue[0].ToString();
                        value = attributeKeyValue.Substring(1);
                        break;

                    default:
                        throw new FormatException(String.Format("The KeyValue string '{0}' does not follow the set standard.", attributeKeyValue));
                    }

                    var added = false;
                    foreach (var tokenAttributeType in textView.Settings.GetTemplateTokenAttributeTypes())
                    {
                        if (tokenAttributeType.Key == key)
                        {
                            added = true;
                            attributes.Add(new TokenAttribute(token, tokenAttributeType, value));
                            break;
                        }
                    }

                    if (added == false)
                    {
                        throw new FormatException(String.Format("The text template token attribute for '{0}' does not exist.", key));
                    }
                }

                token.Attributes = attributes.ToArray();
                yield return(token);

                i += nextMatch.Length;
                nextMatchIdx++;
                previousIdx = i;
            }
        }