public string MakeTextFromSnippet(ScintillaControl sci, AbbrevationSnippet abbrSnippet)
        {
            StringBuilder sbSnippet = new StringBuilder(abbrSnippet.Snippet);

            if (isMonitoring)
                DeactiveMonitorWords();

            if(abbrSnippet.HasImport)
            imports = new List<string>();
            if (abbrSnippet.HasEventHandler)
            eventsHandler = new List<int>();
            if (abbrSnippet.HasAfterCurrentMember)
            AfterCurrentMember = new List<string>();

            _dictCustomList = _setting.customList;

            int pos = sci.CurrentPos;
            int CodePage = sci.CodePage;

            string nl = ASCompletion.Completion.ASComplete.GetNewLineMarker(sci.EOLMode);
            int curLine = sci.LineFromPosition(pos);
            int startLine = sci.PositionFromLine(curLine);

            char ch = (char) sci.CharAt(startLine);
            string tabString = String.Empty;
            while ( Char.IsWhiteSpace( ch))
            {
                if(ch=='\n' || ch=='\r') break;
                tabString += ch;

                ch = (char)sci.CharAt(++startLine);
            }

             sbSnippet.Replace("\r","");
             sbSnippet.Replace("\n", nl + tabString);

             if (abbrSnippet.Arguments == null) return sbSnippet.ToString();

                previousLenPlace = 0;
                indexArgument = 0;
                numCursors = 0;

             MatchCollection mtc = _vocabularyArgument.regArguments.Matches(sbSnippet.ToString());
             int lenght = mtc.Count;
             Match m =null;
             Match var;

             string textClipBoard = "";
             int valueTextClipBoard = 0;

             char[] chars = null;
             int previousPos=0;
             for (int i = 0; i < lenght; i++)
             {
                  m = mtc[i];

                  /// if CodePage is 65001... pos= is position in scintilla text
                  /// and previousLen is position on abbrSnippet
                  if (indexArgument == abbrSnippet.Arguments.Length)
                  {
                      indexArgument = 0;

                          lsVar.Clear();

                  }

                  switch (abbrSnippet.Arguments[indexArgument])
                  {
                      #region Place
                      case WordTypes.place:
                          var = _vocabularyArgument.regPlace.Match(m.Value);
                          if (var.Success)
                          {
                              WordRegion wr = new WordRegion();

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length - var.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, var.Value);

                              previousPos = correctPosString + var.Length;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord + var.Length;
                                  pos += chars.Length + var.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                  int valueLen = Encoding.UTF8.GetByteCount(var.Value);
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord + valueLen;
                                  pos += strLen + valueLen;
                              }

                              wr.type = WordRegion.kind.place;

                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              Words.Add(wr);

                              curWord = wr;
                              curIndWord++;
                              numCursors++;
                              indexArgument++;

                            //  sb.Append(var.Value);
                          }
                          break;
                      #endregion
                      #region cursor
                      case WordTypes.SafeZone:

                          if (m.Value == VocabularyArgument.SafeZone)
                          {
                              WordRegion wr = new WordRegion();
                              wr.type = WordRegionBase.kind.cursor;

                              //string previousTest = textSnippet.Substring(previousLenPlace, m.Index - previousLenPlace);
                              //sb.Append(previousTest);

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length;
                              sbSnippet.Remove(correctPosString, m.Length);

                              previousPos = correctPosString;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord;
                                  pos += chars.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                 // int valueLen = Encoding.UTF8.GetByteCount(var.Value);
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord ;
                                  pos += strLen ;
                              }

                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              Words.Add(wr);
                              curWord = wr;
                              curIndWord++;
                              numCursors++;
                              indexArgument++;

                          }

                          break;
                      #endregion
                      #region createParameters
                      case WordTypes.createParameters:

                          if (m.Value == VocabularyArgument.CREATEPARAMATERS)
                          {
                              WordRegion wr = new WordRegion();
                              wr.type = WordRegionBase.kind.createParameters;

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length;
                              sbSnippet.Remove(correctPosString, m.Length);

                              previousPos = correctPosString;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord;
                                  pos += chars.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord;
                                  pos += strLen;
                              }

                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              Words.Add(wr);
                              curWord = wr;
                              curIndWord++;
                              numCursors++;
                              indexArgument++;

                          }

                          break;
                      #endregion
                      #region clipboard
                      case WordTypes.Clipboard:
                          if (m.Value == VocabularyArgument.CLIPBOARD)
                          {

                              if (textClipBoard.Length==0)
                              {
                                  textClipBoard = System.Windows.Forms.Clipboard.GetText().Trim();
                                  valueTextClipBoard = Encoding.UTF8.GetByteCount(textClipBoard);
                              }

                              int correctPosString = m.Index - previousLenPlace;

                              previousLenPlace += m.Length - textClipBoard.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, textClipBoard);

                              if (CodePage != 65001)
                              {
                                  pos += chars.Length + textClipBoard.Length;
                              }
                              else
                              {

                                  int count = correctPosString - previousPos;
                                  chars = new char[count];
                                  sbSnippet.CopyTo(previousPos, chars, 0, count);
                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );

                                  pos += strLen + valueTextClipBoard;
                              }

                              previousPos = correctPosString + textClipBoard.Length;

                              indexArgument++;

                          }

                          break;
                      #endregion
                      #region showcomp
                      case WordTypes.cmp:

                          var = _vocabularyArgument.regCompType.Match(m.Value);

                          //if (var.Value.Length!=0)
                          if(var.Success)
                          {
                              WordRegion wr = new WordRegion();
                              wr.type = WordRegion.kind.showCompType;

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length - var.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, var.Value);

                              previousPos = correctPosString + var.Length;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord + var.Length;
                                  pos += chars.Length + var.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                  int valueLen = Encoding.UTF8.GetByteCount(var.Value);
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord + valueLen;
                                  pos += strLen + valueLen;
                              }

                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              Words.Add(wr);
                              curWord = wr;

                              curIndWord++;
                              numCursors++;
                              indexArgument++;

                          }

                          break;
                      #endregion
                      #region variable link
                      case WordTypes.var:

                          var = _vocabularyArgument.regVar.Match(m.Value);

                          if (var.Value.Length!=0)
                          {
                              VarWordRegion varRootWR = null;
                              VarDuplicateWordRegion vdwr = null;
                              VarWordRegion vwr = null;
                              WordRegionBase wr;

                              // there is a root?
                              if (lsVar.TryGetValue(var.Value, out varRootWR))
                              {
                                  vdwr = new VarDuplicateWordRegion();
                                  wr = vdwr;
                                  vdwr.type = WordRegionBase.kind.VarLink;
                              }
                              else
                              {

                                  vwr = new VarWordRegion();
                                  vwr.ValueVar = var.Value;
                                  curIndWord++;

                                  string option = m.Value.Substring(var.Index + var.Length);
                                  Match mc = _vocabularyArgument.regList.Match(option);

                                  if (mc.Value.Length!=0)
                                  {
                                      vwr.indList = mc.Value;
                                      vwr.type = WordRegionBase.kind.customList;
                                  }
                                  else if (option.IndexOf("showCmp") != -1)
                                      vwr.type = WordRegion.kind.VarLinkAndComp;
                                  else
                                      vwr.type = WordRegionBase.kind.VarLink;

                                  wr = vwr;

                                  vwr.ChangeVarWord += new VarWordRegion.ChangeVarEventHandler(vwr_ChangeVarWord);
                              }

                              //string previousTest = textSnippet.Substring(previousLenPlace, m.Index - previousLenPlace);
                              //sb.Append(previousTest);

                              // questo serve per cancellare index
                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length - var.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, var.Value);

                              previousPos = correctPosString +  var.Length;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord + var.Length;
                                  pos += chars.Length + var.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                  int valueLen = Encoding.UTF8.GetByteCount(var.Value);
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord + valueLen;
                                  pos += strLen + valueLen;
                              }

                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              curWord = wr;

                              if (varRootWR != null)
                              {
                                  varRootWR.InsertVarDuplicate(vdwr);
                                  //varRootWR.lstWords.Add(vdwr);
                              }
                              else
                              {
                                  lsVar.Add(var.Value, vwr);
                                  Words.Add(wr);
                              }
                              indexArgument++;

                              numCursors++;

                           //   sb.Append(var.Value);
                          }

                          break;
                      #endregion
                      #region mbrname
                      case WordTypes.mbrname:
                          if (m.Value == VocabularyArgument.MbrName)
                          {

                              string strMbrName = "";

                              if (ASContext.Context.CurrentMember != null)
                              {
                                  strMbrName = ASContext.Context.CurrentMember.Name;
                              }

                              int correctPosString = m.Index - previousLenPlace;

                              previousLenPlace += m.Length - strMbrName.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, strMbrName);

                              if (CodePage != 65001)
                              {
                                  pos += chars.Length + strMbrName.Length;
                              }
                              else
                              {

                                  int count = correctPosString - previousPos;
                                  chars = new char[count];
                                  sbSnippet.CopyTo(previousPos, chars, 0, count);
                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );

                                  pos += strLen + strMbrName.Length;
                              }

                              previousPos = correctPosString + strMbrName.Length;

                              indexArgument++;
                          }
                          break;
                      #endregion
                      #region "Browser"
                      case WordTypes.browser:
                          // --------------  browser
                          if (m.Value == VocabularyArgument.BROWSER)
                          {
                              WordRegion wr = new WordRegion();
                              wr.type = WordRegion.kind.browser;

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length;
                              sbSnippet.Remove(correctPosString, m.Length);

                              previousPos = correctPosString;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord;
                                  pos += chars.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord;
                                  pos += strLen ;
                              }

                              //  wr.lineNumber = lineNumber;

                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              Words.Add(wr);
                              curWord = wr;
                              curIndWord++;
                              //  wr.index = curIndWord;
                              numCursors++;
                              indexArgument++;

                          }
                          break;
                      #endregion
                      #region AfterCurrentMember
                      case WordTypes.AfterCurrentMember:

                          var = _vocabularyArgument.regAfterCurrentMember.Match(m.Value);
                          if (var.Value.Length!=0)
                          {

                              if (ASContext.Context.CurrentMember != null)
                              {
                                  if (!AfterCurrentMember.Contains(var.Value))
                                  {
                                      AfterCurrentMember.Add(var.Value);
                                      dontCreateLastWord = true;
                                  }
                                  //strMbrName = ASContext.Context.CurrentMember.Name;
                              }

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length;
                              sbSnippet.Remove(correctPosString, m.Length);

                              previousPos = correctPosString;

                              if (CodePage != 65001)
                              {

                                  pos += chars.Length;
                              }
                              else
                              {
                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );

                                  pos += strLen;
                              }

                              indexArgument++;

                          }
                          break;
                      #endregion
                      #region Custom list
                      case WordTypes.list:
                          //-------- CUSTOM LIST COMPLETITION

                          var = _vocabularyArgument.regList.Match(m.Value);

                          if (var.Value.Length!=0)
                          {
                              WordCustomList wr = new WordCustomList();
                              wr.type = WordRegion.kind.customList;
                              string value = "";
                              List<string> ls;
                              if (_dictCustomList.TryGetValue(var.Value, out ls))
                              {

                                  if(ls.Count>0)
                                  value = ls[0];
                              }

                              int correctPosString = m.Index - previousLenPlace;

                              int count = correctPosString - previousPos;
                              chars = new char[count];

                              sbSnippet.CopyTo(previousPos, chars, 0, count);
                              previousLenPlace += m.Length - value.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, value);

                              previousPos = correctPosString + value.Length;

                              if (CodePage != 65001)
                              {
                                  wr.startWord = pos + chars.Length;
                                  wr.endWord = wr.startWord + value.Length;
                                  pos += chars.Length + value.Length;
                              }
                              else
                              {

                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );
                                  int valueLen = Encoding.UTF8.GetByteCount(value);
                                  wr.startWord = pos + strLen;
                                  wr.endWord = wr.startWord + valueLen;
                                  pos += strLen + valueLen;
                              }

                              //  wr.lineNumber = lineNumber;
                              wr.indList = var.Value;
                              if (curWord != null)
                              {
                                  curWord.NextWord = wr;
                                  secondLastWord = curWord;
                              }

                              Words.Add(wr);
                              curWord = wr;
                              curIndWord++;
                              //   wr.index = curIndWord;
                              numCursors++;
                              indexArgument++;

                          }

                          break;
                      #endregion
                      #region import
                      case WordTypes.import:
                          var = _vocabularyArgument.regImport.Match(m.Value);

                          if (var.Value.Length!=0)
                          {

                              int correctPosString = m.Index - previousLenPlace;

                              previousLenPlace += m.Length - var.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, var.Value);

                              if (CodePage != 65001)
                              {
                                  pos += chars.Length + var.Length;
                              }
                              else
                              {

                                  int count = correctPosString - previousPos + var.Value.Length;
                                  chars = new char[count];
                                  sbSnippet.CopyTo(previousPos, chars, 0, count);
                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );

                                  pos += strLen ;
                              }

                              previousPos = correctPosString + var.Length;

                              if (!imports.Contains(var.Value))
                                  imports.Add(var.Value);

                              indexArgument++;

                          }
                          break;
                      #endregion
                      #region EventHandler
                      case WordTypes.EventHandler:
                          var = _vocabularyArgument.regEventHandler.Match(m.Value);

                          if (var.Value.Length!=0)
                          {

                              int correctPosString = m.Index - previousLenPlace;

                              previousLenPlace += m.Length - var.Length;
                              sbSnippet.Remove(correctPosString, m.Length);
                              sbSnippet.Insert(correctPosString, var.Value);

                              if (CodePage != 65001)
                              {
                                  pos += chars.Length + var.Length;
                              }
                              else
                              {

                                  int count = correctPosString - previousPos + var.Value.Length;
                                  chars = new char[count];
                                  sbSnippet.CopyTo(previousPos, chars, 0, count);
                                  int strLen = Encoding.UTF8.GetByteCount(
                                      chars
                                      );

                                  pos += strLen;

                              }
                              eventsHandler.Add(pos);
                              previousPos = correctPosString + var.Length;

                              indexArgument++;

                          }
                          break;
                      #endregion
                  }

             }

            if (!dontCreateLastWord)
            ConvertLastWord();

            return sbSnippet.ToString();
        }
        public void InsertVarDuplicate(VarDuplicateWordRegion var)
        {
            var.rootWord = this;
            if (NextVarDuplicate == null)
            {
                NextVarDuplicate = var;
                lastVar = var;
            }
            else
            {
                secondLast = lastVar;
                lastVar.NextVarDuplicate = var;
                lastVar = var;

            }
        }
        public void SetLastVar(VarDuplicateWordRegion lastWord)
        {
            lastWord.rootWord = this;

            if (secondLast != null)
                secondLast.NextVarDuplicate = lastWord;
            else
            {
                NextVarDuplicate = lastWord;
                lastVar = lastWord;
            }
        }
 void IDisposable.Dispose()
 {
     lastVar = null;
     NextWord = null;
     NextVarDuplicate = null;
     VarNotify = null;
 }