<commit_before>﻿using System;
using System.IO;
using EnvDTE;
using EnvDTE80;

namespace STDL.Code
{
    class STDLClassFileGenerator
    {
        private readonly DTE2 _applicationObject;
        private readonly STDLClass _clazz;
        private static readonly string _folder = "stdl";

        public STDLClassFileGenerator(DTE2 applicationObject, STDLClass clazz)
        {
            _applicationObject = applicationObject;
            _clazz = clazz;
        }

        public void CreateFile()
        {
            CreateSTDLFolder();
            string projectPath = _applicationObject.ActiveDocument.Path;
            string filename = Path.Combine(Path.Combine(projectPath, _folder), _clazz.ClassName + ".stdl");
            string tmpFilename = Path.Combine(projectPath, DateTime.Now.Ticks + ".stdl");

            GenerateTemporaryFile(tmpFilename);

            ProjectItem item = _applicationObject.ItemOperations.AddExistingItem(tmpFilename);
            Window window = item.Open(Constants.vsViewKindTextView);
            item.SaveAs(filename);
            window.Activate();
            File.Delete(tmpFilename);
            
            _applicationObject.ActiveDocument.ProjectItem.ContainingProject.Save(_applicationObject.ActiveDocument.ProjectItem.ContainingProject.FullName);
        }

        private void GenerateTemporaryFile(string tmpFilename)
        {
            if (File.Exists(tmpFilename))
            {
                File.Delete(tmpFilename);
            }
            StreamWriter stream = File.CreateText(tmpFilename);

            stream.Write(_clazz.ToString());
            
            stream.Flush();
            stream.Close();
        }

        private void CreateSTDLFolder()
        {
            try
            {
                _applicationObject.ActiveDocument.ProjectItem.ContainingProject.ProjectItems.AddFolder(_folder, "");
            }
            catch
            {
                //Folder already exists
                return;
            }
        }
    }
}<commit_msg>Improved folder generation.<commit_after>﻿using System.IO;
using EnvDTE;
using EnvDTE80;

namespace STDL.Code
{
    class STDLClassFileGenerator
    {
        private readonly DTE2 _applicationObject;
        private readonly STDLClass _clazz;
        private static readonly string _folder = "stdl";

        public STDLClassFileGenerator(DTE2 applicationObject, STDLClass clazz)
        {
            _applicationObject = applicationObject;
            _clazz = clazz;
        }

        public void CreateFile()
        {
            ProjectItem folder = CreateSTDLFolder();
            string projectPath = _applicationObject.ActiveDocument.Path;
            string filename = Path.Combine(Path.Combine(projectPath, _folder), _clazz.ClassName + ".stdl");

            GenerateFile(filename);
            
            ProjectItem item = folder.ProjectItems.AddFromFile(filename);
            _applicationObject.ActiveDocument.ProjectItem.ContainingProject.Save(_applicationObject.ActiveDocument.ProjectItem.ContainingProject.FullName);
            Window window = item.Open(Constants.vsViewKindTextView);
            window.Activate();
        }

        private void GenerateFile(string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            StreamWriter stream = File.CreateText(filename);

            stream.Write(_clazz.ToString());
            
            stream.Flush();
            stream.Close();
        }

        private ProjectItem CreateSTDLFolder()
        {
            foreach (ProjectItem projectItem in _applicationObject.ActiveDocument.ProjectItem.ContainingProject.ProjectItems)
            {
                if(_folder.Equals(projectItem.Name))
                {
                    return projectItem;
                }
            };
            return _applicationObject.ActiveDocument.ProjectItem.ContainingProject.ProjectItems.AddFolder(_folder, "");
        }
    }
}
{'fraction_non_alphanumeric': 0.07858741606565531, 'fraction_numerical': 0.001989554837105198, 'mean_word_length': 1.9704579025110782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1448625', 'n_tokens_mistral': 980, 'n_tokens_neox': 956, 'n_words': 172}