<commit_before>﻿using System.Linq;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;

namespace DanTup.DartVS.Providers
{
	// TODO: Currently this breaks when VS converts 4 spaces to 1 tab. To fix, we really need to
	// know if VS is going to covert x spaces to tabs (and then count a tab as that many characters)
	// However; we really also need to be able to register Dart as a language and set it to default to
	// 2 spaces (no tabs); so we can fix this all up toegther if I ever figure out how to do it.

	// TODO: Remove this export when it's ready to use.
	//[Export(typeof(ISmartIndentProvider))]
	[ContentType(DartContentTypeDefinition.DartContentType)]
	class SmartIndentProvider : ISmartIndentProvider
	{
		public ISmartIndent CreateSmartIndent(ITextView textView)
		{
			return textView.Properties.GetOrCreateSingletonProperty(() => new SmartIndent());
		}
	}

	class SmartIndent : ISmartIndent
	{
		public int? GetDesiredIndentation(ITextSnapshotLine line)
		{
			// If we're on the first line, we can't really do anything clever.
			if (line.LineNumber == 0)
				return 0;

			var snapshot = line.Snapshot;

			// Walk up previous lines trying to find the first non-blank one.
			var previousNonBlankLine = snapshot.GetLineFromLineNumber(line.LineNumber - 1);
			while (previousNonBlankLine.LineNumber >= 1 && previousNonBlankLine.GetText().Trim().Length == 0)
				previousNonBlankLine = snapshot.GetLineFromLineNumber(previousNonBlankLine.LineNumber - 1);

			// If we didn't find an actual non-blank line, we can't really do anything clever.
			if (previousNonBlankLine.GetText().Trim() == "")
				return 0;

			var previousLineText = previousNonBlankLine.GetText();
			var thisLineText = line.GetText();
			var previousLineIndent = previousLineText.TakeWhile(char.IsWhiteSpace).Count();

			if (previousLineText.TrimEnd().EndsWith("{"))
				return previousLineIndent + 2;
			else if (thisLineText.TrimStart().StartsWith("}") && previousLineIndent >= 2)
				return previousLineIndent - 2;
			else
				return previousLineIndent;
		}

		public void Dispose()
		{
		}
	}
}
<commit_msg>Handle indenting better.<commit_after>﻿using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;

namespace DanTup.DartVS.Providers
{
	[Export(typeof(ISmartIndentProvider))]
	[ContentType(DartContentTypeDefinition.DartContentType)]
	class SmartIndentProvider : ISmartIndentProvider
	{
		public ISmartIndent CreateSmartIndent(ITextView textView)
		{
			var tabSize = textView.Options.Parent.IsOptionDefined("Tabs/TabSize", true) ? textView.Options.Parent.GetOptionValue<int>("Tabs/TabSize") : 2;
			return textView.Properties.GetOrCreateSingletonProperty(() => new SmartIndent(tabSize));
		}
	}

	class SmartIndent : ISmartIndent
	{
		int tabSize;
		public SmartIndent(int tabSize)
		{
			this.tabSize = tabSize;
		}

		public int? GetDesiredIndentation(ITextSnapshotLine line)
		{
			// If we're on the first line, we can't really do anything clever.
			if (line.LineNumber == 0)
				return 0;

			var snapshot = line.Snapshot;

			// Walk up previous lines trying to find the first non-blank one.
			var previousNonBlankLine = snapshot.GetLineFromLineNumber(line.LineNumber - 1);
			while (previousNonBlankLine.LineNumber >= 1 && previousNonBlankLine.GetText().Trim().Length == 0)
				previousNonBlankLine = snapshot.GetLineFromLineNumber(previousNonBlankLine.LineNumber - 1);

			// If we didn't find an actual non-blank line, we can't really do anything clever.
			if (previousNonBlankLine.GetText().Trim() == "")
				return 0;

			var previousLineText = previousNonBlankLine.GetText();
			var previousLineIndent = previousLineText.Replace("\t", new string(' ', tabSize)).TakeWhile(char.IsWhiteSpace).Count();

			// If we started a block on the previous line; then add indent.
			if (previousLineText.TrimEnd().EndsWith("{"))
				return previousLineIndent + tabSize;
			else
				return previousLineIndent;
		}

		public void Dispose()
		{
		}
	}
}

{'fraction_non_alphanumeric': 0.10012091898428054, 'fraction_numerical': 0.005078597339782346, 'mean_word_length': 4.350582147477361, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, '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': '2362532', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1246, 'n_words': 347}