Skip to content

Commit

Permalink
Merge pull request #20 from Lombiq/issue/VSIX-40
Browse files Browse the repository at this point in the history
VSIX-40: Make Dependency Injector support class declarations with 0 indentation (for file scoped namespaces)
  • Loading branch information
sarahelsaig authored Mar 16, 2022
2 parents 7f9073e + b7126c7 commit 3089db9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public interface IDependencyInjector
}

public class DependencyInjector : IDependencyInjector
{
{
// We could later add a feature to read this out from VS settings somehow, or add logic to determine it, but
// virtually everyone uses spaces instead of tabs with a size of 4 characters in the Orchard world.
public const int DefaultIndentSize = 4;

public IResult Inject(Document document, DependencyInjectionData dependencyInjectionData)
{
// We should never get an exception here. This is just to ensure we access DTE on the main thread and get
Expand Down Expand Up @@ -157,25 +161,24 @@ private static void GetConstructorLineIndex(DependencyInjectionContext context)

private static void CreateConstructor(DependencyInjectionContext context)
{
var classStartIndentSize = GetIndentSizeOfLine(context.CodeLines[context.ClassStartLineIndex]);
var classBodyIndentSize = GetClassBodyIndentSize(context);
var createConstructorFromIndex = context.ClassStartLineIndex + (context.BraceStyle == BraceStyles.OpenInNewLine ? 2 : 1);

// Add two empty lines before and after to separate the constructor from the field and the other parts of the code.
var constructorCodeLines = context.BraceStyle == BraceStyles.OpenInNewLine ?
new[]
var constructorCodeLines = context.BraceStyle == BraceStyles.OpenInNewLine
? new[]
{
string.Empty,
IndentText(classStartIndentSize, 2, "public " + context.ClassName + "()"),
IndentText(classStartIndentSize, 2, "{"),
IndentText(classStartIndentSize, 2, "}"),
IndentText(classBodyIndentSize, 1, "public " + context.ClassName + "()"),
IndentText(classBodyIndentSize, 1, "{"),
IndentText(classBodyIndentSize, 1, "}"),
string.Empty,
}
:
new[]
: new[]
{
string.Empty,
IndentText(classStartIndentSize, 2, "public " + context.ClassName + "() {"),
IndentText(classStartIndentSize, 2, "}"),
IndentText(classBodyIndentSize, 1, "public " + context.ClassName + "() {"),
IndentText(classBodyIndentSize, 1, "}"),
string.Empty,
};

Expand All @@ -190,8 +193,8 @@ private static void CreateConstructor(DependencyInjectionContext context)
private static void InsertConstructorCodeLine(DependencyInjectionContext context)
{
var constructorLine = context.CodeLines[context.ConstructorLineIndex];
var constructorIndentSize = GetIndentSizeOfLine(constructorLine);
var constructorCodeLine = IndentText(constructorIndentSize, 1.5, context.FieldName + " = " + context.VariableName + ";");
var constructorIndentSize = GetIndentSizeOfLine(constructorLine) + DefaultIndentSize;
var constructorCodeLine = IndentText(constructorIndentSize, 1, context.FieldName + " = " + context.VariableName + ";");

var constructorCodeLineInserted = false;
var i = context.ConstructorLineIndex - 1;
Expand Down Expand Up @@ -262,7 +265,7 @@ private static void InsertInjectionToConstructor(DependencyInjectionContext cont
var afterClosing = context.CodeLines[i].Substring(indexOfClosing);

context.CodeLines.RemoveAt(i);
context.CodeLines.Insert(i, IndentText(indentSize, 1.5, injection + afterClosing));
context.CodeLines.Insert(i, IndentText(indentSize + DefaultIndentSize, 1, injection + afterClosing));
context.CodeLines.Insert(i, beforeClosing + ",");
injectionInserted = true;
}
Expand All @@ -271,9 +274,9 @@ private static void InsertInjectionToConstructor(DependencyInjectionContext cont

private static void InsertPrivateField(DependencyInjectionContext context)
{
var classStartLine = context.CodeLines[context.ClassStartLineIndex];
var indentSize = GetIndentSizeOfLine(classStartLine);
var privateFieldLine = new string(' ', indentSize * 2) + "private readonly " + context.FieldType + " " + context.FieldName + ";";
var classBodyIndentSize = GetClassBodyIndentSize(context);
var privateFieldLine =
IndentText(classBodyIndentSize, 1, "private readonly " + context.FieldType + " " + context.FieldName + ";");

var i = context.ClassStartLineIndex + (context.BraceStyle == BraceStyles.OpenInNewLine ? 1 : 0);
var privateFieldInserted = false;
Expand Down Expand Up @@ -310,9 +313,12 @@ private static int GetIndentSizeOfLine(string codeLine)

return indentSize;
}

private static string IndentText(int baseIndentSize, double indentSizeMultiplier, string text) =>
new string(' ', Convert.ToInt32(baseIndentSize * indentSizeMultiplier)) + text;

private static int GetClassBodyIndentSize(DependencyInjectionContext context) =>
GetIndentSizeOfLine(context.CodeLines[context.ClassStartLineIndex]) + DefaultIndentSize;

private static string IndentText(int baseIndentSize, double indentSizeMultiplier, string text) =>
new string(' ', Convert.ToInt32(baseIndentSize * indentSizeMultiplier)) + text;

private enum BraceStyles
{
Expand Down
2 changes: 1 addition & 1 deletion tools/Lombiq.Analyzers

0 comments on commit 3089db9

Please sign in to comment.