Exemple #1
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		void IVsCodeBehindCodeGenerator.BeginGenerate(string document, string codeBehindFile, string className_Full, bool create)
		{
			DisposeGenerateState();

			_itemCode = VsHierarchyItem.CreateFromMoniker(codeBehindFile, _hierarchy);
			_itemDesigner = GetDesignerItem(_itemCode, false);
			_create = create;
			_className_Full = className_Full;

			if (_itemCode != null)
			{
				// Get the CodeDomProvider for the language (MergedCodeDomProvider C#/VB)
				_codeDomProvider = CreateCodeDomProvider(_itemCode.VsItemID);

				if (_codeDomProvider != null)
				{
					// Get the field names so we can preserve location and access
					bool caseSensitive = IsCaseSensitive(_codeDomProvider);

					_codeFields = GetFieldNames(_itemCode, _className_Full, caseSensitive, false, 30);
					_designerFields = GetFieldNames(_itemDesigner, _className_Full, caseSensitive, false, 0);

					// Generate the class
					string designerContents = _itemDesigner.GetDocumentText();
					TextReader reader = new StringReader(designerContents);
					_ccu = _codeDomProvider.Parse(reader);
					GenerateClass();
				}
			}
		}
        protected override void Initialize()
        {
            var fileLastWriteTime = File.GetLastWriteTime(_filePath);

            // нет необходимости парсить файл повторно если документ не отмечен как измененный и дата его сохранения не изменилась
            if (fileLastWriteTime > _fileTime || ProjectItem.IsDirty || CompileUnit == null)
            {
                var content = ReadSourceFromBuffer();

                TextReader reader = null;

                try
                {
                    reader = (content == null)
                                                ? (TextReader) new StreamReader(_filePath, true)
                                                : (TextReader) new StringReader(content);

                    CompileUnit = _codeDomProvider.Parse(reader);
                    _fileTime   = fileLastWriteTime;

                    reader.Close();
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
            }
        }
        protected override void Initialize()
        {
            var projectInfo = Nemerle.VisualStudio.Project.ProjectInfo.FindProject(_fileNode.Url);

            if (projectInfo != null)
            {
                projectInfo.Engine.ProcessPendingTypesTreeRequest();
            }

            CompileUnit = _codeDomProvider.Parse(null);
        }
Exemple #4
0
    private CodeCompileUnit GetExistingCode(string filepath)
    {
        if (filepath != "" && File.Exists(filepath))
        {
            CodeDomProvider provider   = CodeDomProvider.CreateProvider("CSharp");
            CodeCompileUnit targetUnit = new CodeCompileUnit();

            using (StreamReader sourceReader = new StreamReader(filepath))
            {
                targetUnit = provider.Parse(sourceReader);
            }
            return(targetUnit);
        }
        return(null);
    }
Exemple #5
0
        //Adapted from CodeDomFileDescriptionTemplate.cs
        //TODO: Remove need to have a namespace and type (i.e. translate fragments)
        public override string CreateContent(Project project, Dictionary <string, string> tags, string language)
        {
            //get target language's ICodeGenerator
            if (language == null || language == "")
            {
                throw new InvalidOperationException(GettextCatalog.GetString("Language not defined in CodeDom based template."));
            }

            CodeDomProvider provider = GetCodeDomProvider(language);

            //parse the source code
            if (tempSubstitutedContent == null)
            {
                throw new Exception(GettextCatalog.GetString("Expected ModifyTags to be called before CreateContent"));
            }

            CodeCompileUnit ccu;

            using (StringReader sr = new StringReader(tempSubstitutedContent)) {
                try {
                    ccu = parserProvider.Parse(sr);
                } catch (NotImplementedException) {
                    throw new InvalidOperationException(GettextCatalog.GetString("Invalid Code Translation template: the CodeDomProvider of the source language '{0}' has not implemented the Parse method.", language));
                } catch (Exception ex) {
                    LoggingService.LogError("Unparseable template: '" + tempSubstitutedContent + "'.", ex);
                    throw;
                }
            }

            foreach (CodeNamespace cns in ccu.Namespaces)
            {
                cns.Name = CodeDomFileDescriptionTemplate.StripImplicitNamespace(project, tags, cns.Name);
            }

            tempSubstitutedContent = null;

            //and generate the code
            CodeGeneratorOptions options = new CodeGeneratorOptions();

            options.IndentString = "\t";
            options.BracingStyle = "C";

            string txt = string.Empty;

            using (StringWriter sw = new StringWriter()) {
                provider.GenerateCodeFromCompileUnit(ccu, sw, options);
                txt = sw.ToString();
            }

            if (showAutogenerationNotice)
            {
                return(txt);
            }

            //remove auto-generation notice
            int i = txt.IndexOf("</autogenerated>");

            if (i == -1)
            {
                return(txt);
            }
            i = txt.IndexOf('\n', i);
            if (i == -1)
            {
                return(txt);
            }
            i = txt.IndexOf('\n', i + 1);
            if (i == -1)
            {
                return(txt);
            }

            return(txt.Substring(i + 1));
        }