From 0f7727e31b183cd158a3b04c412421348c0c3c5e Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Sun, 27 Mar 2016 23:29:38 -0700 Subject: [PATCH] Fix #235 --- src/dfmt/editorconfig.d | 10 +++++++--- src/dfmt/main.d | 30 ++++++++++++++++-------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/dfmt/editorconfig.d b/src/dfmt/editorconfig.d index b4e5501..b25b9ef 100644 --- a/src/dfmt/editorconfig.d +++ b/src/dfmt/editorconfig.d @@ -122,14 +122,18 @@ EC getConfigFor(EC)(string path) { import std.stdio : File; import std.regex : regex, match; - import std.path : globMatch, dirName, baseName, pathSplitter, buildPath; + import std.path : globMatch, dirName, baseName, pathSplitter, buildPath, + absolutePath; import std.algorithm : reverse, map, filter; import std.array : array; + import std.file : isDir; EC result; EC[][] configs; - string dir = dirName(path); - immutable string fileName = baseName(path); + immutable expanded = absolutePath(path); + immutable bool id = isDir(expanded); + immutable string dir = dirName(expanded); + immutable string fileName = id ? "dummy.d" : baseName(expanded); string[] pathParts = cast(string[]) pathSplitter(dir).array(); for (size_t i = pathParts.length; i > 1; i--) { diff --git a/src/dfmt/main.d b/src/dfmt/main.d index 229b193..da2182b 100644 --- a/src/dfmt/main.d +++ b/src/dfmt/main.d @@ -104,16 +104,6 @@ else args.popFront(); immutable bool readFromStdin = args.length == 0; - immutable string filePath = createFilePath(readFromStdin, readFromStdin ? null : args[0]); - Config config; - config.initializeWithDefaults(); - Config fileConfig = getConfigFor!Config(filePath); - fileConfig.pattern = "*.d"; - config.merge(fileConfig, filePath); - config.merge(optConfig, filePath); - - if (!config.isValid()) - return 1; File output = stdout; version (Windows) @@ -121,7 +111,7 @@ else // On Windows, set stdout to binary mode (needed for correct EOL writing) // See Phobos' stdio.File.rawWrite { - import std.stdio:fileno, _O_BINARY, setmode; + import std.stdio : fileno, _O_BINARY, setmode; immutable fd = fileno(output.getFP()); setmode(fd, _O_BINARY); @@ -138,6 +128,11 @@ else if (readFromStdin) { + Config config; + config.initializeWithDefaults(); + config.merge(optConfig, null); + if (!config.isValid()) + return 1; ubyte[4096] inputBuffer; ubyte[] b; while (true) @@ -164,11 +159,17 @@ else { inplace = true; foreach (string name; dirEntries(path, "*.d", SpanMode.depth)) - { args ~= name; - } continue; } + Config config; + config.initializeWithDefaults(); + Config fileConfig = getConfigFor!Config(path); + fileConfig.pattern = "*.d"; + config.merge(fileConfig, path); + config.merge(optConfig, path); + if (!config.isValid()) + return 1; File f = File(path); buffer = new ubyte[](cast(size_t) f.size); f.rawRead(buffer); @@ -223,7 +224,8 @@ Formatting Options: --split_operator_at_line_end --compact_labeled_statements --template_constraint_style - `, optionsToString!(typeof(Config.dfmt_template_constraint_style))()); + `, + optionsToString!(typeof(Config.dfmt_template_constraint_style))()); } private string createFilePath(bool readFromStdin, string fileName)