Skip to content

Commit

Permalink
Normalise line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricel committed Apr 12, 2021
1 parent 5b0eedb commit f1e321c
Showing 1 changed file with 87 additions and 87 deletions.
174 changes: 87 additions & 87 deletions CliFx/Suggestions/SuggestionService.cs
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
using CliFx.Infrastructure;
using CliFx.Input;
using CliFx.Schema;
using CliFx.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CliFx.Suggestions
{
internal class SuggestionService
{
private ApplicationSchema _applicationSchema;
private readonly IFileSystem _fileSystem;
private readonly IReadOnlyList<EnvironmentVariableInput> _environmentVariableInputs;

public SuggestionService(ApplicationSchema applicationSchema, IFileSystem fileSystem, IReadOnlyList<EnvironmentVariableInput> environmentVariableInputs)
{
_applicationSchema = applicationSchema;
_fileSystem = fileSystem;
_environmentVariableInputs = environmentVariableInputs;
}

using CliFx.Infrastructure;
using CliFx.Input;
using CliFx.Schema;
using CliFx.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CliFx.Suggestions
{
internal class SuggestionService
{
private ApplicationSchema _applicationSchema;
private readonly IFileSystem _fileSystem;
private readonly IReadOnlyList<EnvironmentVariableInput> _environmentVariableInputs;

public SuggestionService(ApplicationSchema applicationSchema, IFileSystem fileSystem, IReadOnlyList<EnvironmentVariableInput> environmentVariableInputs)
{
_applicationSchema = applicationSchema;
_fileSystem = fileSystem;
_environmentVariableInputs = environmentVariableInputs;
}

public bool ShouldInstallHooks(CommandInput commandInput)
{
return commandInput.Options.Any(p => p.Identifier == "install");
}

public IEnumerable<string> GetSuggestions(CommandInput commandInput)
{
var text = ExtractCommandText(commandInput);
var suggestArgs = CommandLineSplitter.Split(text).Skip(1); // ignore the application name

var suggestInput = CommandInput.Parse(
suggestArgs.ToArray(),
_environmentVariableInputs.ToDictionary(p => p.Name, p => p.Value),
_applicationSchema.GetCommandNames());

var commandMatch = _applicationSchema.Commands
.FirstOrDefault(p => string.Equals(p.Name, suggestInput.CommandName, StringComparison.OrdinalIgnoreCase));

// suggest a command name if we don't have an exact match
if (commandMatch == null)
{
return _applicationSchema.GetCommandNames()
.Where(p => p.StartsWith(suggestInput.CommandName, StringComparison.OrdinalIgnoreCase))
.OrderBy(p => p)
.ToList();
}

return NoSuggestions();
}

private string ExtractCommandText(CommandInput input)
{
// Accept command line arguments via environment variable as a workaround to powershell escape sequence shennidgans
var commandCacheVariable = input.Options.FirstOrDefault(p => p.Identifier == "envvar")?.Values[0];

if (commandCacheVariable == null)
{
// ignore cursor position as we don't know what the original user input string really is
return string.Join(" ", input.OriginalCommandLine.Where(arg => !IsDirective(arg)));
}

var command = input.EnvironmentVariables.FirstOrDefault(p => string.Equals(p.Name, commandCacheVariable))?.Value ?? "";
var cursorPositionText = input.Options.FirstOrDefault(p => p.Identifier == "cursor")?.Values[0];
var cursorPosition = command.Length;

if (int.TryParse(cursorPositionText, out cursorPosition) && cursorPosition < command.Length)
{
return command.Remove(cursorPosition);
}
return command;
}

private static bool IsDirective(string arg)
{
return arg.StartsWith('[') && arg.EndsWith(']');
}

private static List<string> NoSuggestions()
{
return new List<string>();
}
}
}

}

public IEnumerable<string> GetSuggestions(CommandInput commandInput)
{
var text = ExtractCommandText(commandInput);
var suggestArgs = CommandLineSplitter.Split(text).Skip(1); // ignore the application name

var suggestInput = CommandInput.Parse(
suggestArgs.ToArray(),
_environmentVariableInputs.ToDictionary(p => p.Name, p => p.Value),
_applicationSchema.GetCommandNames());

var commandMatch = _applicationSchema.Commands
.FirstOrDefault(p => string.Equals(p.Name, suggestInput.CommandName, StringComparison.OrdinalIgnoreCase));

// suggest a command name if we don't have an exact match
if (commandMatch == null)
{
return _applicationSchema.GetCommandNames()
.Where(p => p.StartsWith(suggestInput.CommandName, StringComparison.OrdinalIgnoreCase))
.OrderBy(p => p)
.ToList();
}

return NoSuggestions();
}

private string ExtractCommandText(CommandInput input)
{
// Accept command line arguments via environment variable as a workaround to powershell escape sequence shennidgans
var commandCacheVariable = input.Options.FirstOrDefault(p => p.Identifier == "envvar")?.Values[0];

if (commandCacheVariable == null)
{
// ignore cursor position as we don't know what the original user input string really is
return string.Join(" ", input.OriginalCommandLine.Where(arg => !IsDirective(arg)));
}

var command = input.EnvironmentVariables.FirstOrDefault(p => string.Equals(p.Name, commandCacheVariable))?.Value ?? "";
var cursorPositionText = input.Options.FirstOrDefault(p => p.Identifier == "cursor")?.Values[0];
var cursorPosition = command.Length;

if (int.TryParse(cursorPositionText, out cursorPosition) && cursorPosition < command.Length)
{
return command.Remove(cursorPosition);
}
return command;
}

private static bool IsDirective(string arg)
{
return arg.StartsWith('[') && arg.EndsWith(']');
}

private static List<string> NoSuggestions()
{
return new List<string>();
}
}
}

0 comments on commit f1e321c

Please sign in to comment.