/// <summary> /// Attempts to run code cleanup on the specified document. /// </summary> /// <param name="document">The document for cleanup.</param> internal void Cleanup(Document document) { if (!_codeCleanupAvailabilityLogic.ShouldCleanup(document, true)) { return; } // Make sure the document to be cleaned up is active, required for some commands like format document. document.Activate(); if (_package.IDE.ActiveDocument != document) { OutputWindowHelper.WriteLine(document.Name + " did not complete activation before cleaning started."); } _undoTransactionHelper.Run( delegate { _package.IDE.StatusBar.Text = String.Format("EditorConfig is cleaning '{0}'...", document.Name); // Perform the set of configured cleanups based on the language. RunCodeCleanupGeneric(document); _package.IDE.StatusBar.Text = String.Format("EditorConfig cleaned '{0}'.", document.Name); }, delegate(Exception ex) { OutputWindowHelper.WriteLine(String.Format("EditorConfig stopped cleaning '{0}': {1}", document.Name, ex)); _package.IDE.StatusBar.Text = String.Format("EditorConfig stopped cleaning '{0}'. See output window for more details.", document.Name); }); }
public void Accept(string filePath, IEnumerable <EslintMessage> messages) { try { UpdateMessages(filePath, messages); } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } }
private void Cancel() { try { _source?.Cancel(); } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } finally { _source?.Dispose(); _source = null; } }
/// <summary> /// Performs initial cleanup methods on the active document. /// </summary> internal void Execute() { if (_doc == null || _textDoc == null) { return; } if (_doc.ReadOnly) { OutputWindowHelper.WriteLine(_doc.Name + " is readonly and thus ignored."); } var settings = _settings.Properties.Aggregate(new StringBuilder(), (sb, kv) => sb.AppendFormat(" {0}={1}", kv.Key, kv.Value), sb => sb.ToString()); var loadMessage = string.Format("{0}: opened with settings: {1}", _doc.Name, settings); _ide.StatusBar.Text = "EditorConfig setttings applied."; OutputWindowHelper.WriteLine(loadMessage); return; //For now lets not do initial unasked cleanup, cleanup only on save or format document //Assume files are opened for reading only // Make sure the document to be cleaned up is active, required for some commands like format document. _doc.Activate(); if (_ide.ActiveDocument != _doc) { OutputWindowHelper.WriteLine(_doc.Name + " did not complete activation before cleaning started."); } new UndoTransactionHelper(_ide, "EditorConfig Cleanup").Run( delegate { _ide.StatusBar.Text = String.Format("EditorConfig is cleaning '{0}'...", _doc.Name); RunInitialCleanup(); _ide.StatusBar.Text = String.Format("EditorConfig cleaned '{0}'.", _doc.Name); }, delegate(Exception ex) { OutputWindowHelper.WriteLine(String.Format("EditorConfig stopped cleaning '{0}': {1}", _doc.Name, ex)); _ide.StatusBar.Text = String.Format("EditorConfig stopped cleaning '{0}'. See output window for more details.", _doc.Name); }); }
private async void OnFileActionOccurred(object sender, TextDocumentFileActionEventArgs e) { switch (e.FileActionType) { case FileActionTypes.ContentSavedToDisk: case FileActionTypes.ContentLoadedFromDisk: await Analyze(FilePath).ConfigureAwait(false); break; case FileActionTypes.DocumentRenamed: Rename(FilePath, e.FilePath); break; default: OutputWindowHelper.WriteLine("info: unrecognized file action type"); break; } }
public IEnumerable <ITagSpan <IErrorTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (0 == spans.Count || null == Snapshot || false == Snapshot.Markers.Any()) { return(Enumerable.Empty <ITagSpan <IErrorTag> >()); } try { return(Snapshot.Markers .Where(marker => spans.IntersectsWith(new NormalizedSnapshotSpanCollection(marker.Span))) .Select(marker => new TagSpan <IErrorTag>(marker.Span, new LinterTag(marker.Message)))); } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } return(Enumerable.Empty <ITagSpan <IErrorTag> >()); }
/// <summary> /// Called when a DispatcherUnhandledException is raised by Visual Studio. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="DispatcherUnhandledExceptionEventArgs" /> instance containing the event data.</param> private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { OutputWindowHelper.WriteLine("EditorConfig's diagnostics mode caught the following unhandled exception in Visual Studio--" + Environment.NewLine + e.Exception); e.Handled = true; }
public async Task LintAsync(ILinterProvider provider, string filePath, CancellationToken token) { try { await _mutex.WaitAsync(token).ConfigureAwait(false); try { token.ThrowIfCancellationRequested(); var directoryPath = Path.GetDirectoryName(filePath) ?? throw new Exception($"exception: could not get directory for file {filePath}"); var eslintPath = EslintHelper.GetEslintPath(directoryPath); var arguments = string.Join(" ", QuoteArgument(filePath), EslintHelper.GetArguments(directoryPath)); var output = await RunAsync(eslintPath, arguments, token) .ConfigureAwait(false); token.ThrowIfCancellationRequested(); if (string.IsNullOrEmpty(output)) { throw new Exception("exception: linter returned empty result"); } IEnumerable <EslintResult> results = new List <EslintResult>(); try { results = JsonConvert.DeserializeObject <IEnumerable <EslintResult> >(output); } catch (Exception e) { OutputWindowHelper.WriteLine( "exception: error trying to deserialize output:" + Environment.NewLine + output); OutputWindowHelper.WriteLine(e.Message); } var messages = ProcessResults(results); token.ThrowIfCancellationRequested(); provider.Accept(filePath, messages); } catch (OperationCanceledException) { } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } finally { _mutex.Release(); } } catch (OperationCanceledException) { } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } }
private static Task <string> RunAsync(string eslintPath, string arguments, CancellationToken token) { return(Task.Run(() => { var startInfo = new ProcessStartInfo(eslintPath, arguments) { CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, StandardErrorEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8 }; var process = new Process { StartInfo = startInfo }; string error = null; string output = null; process.ErrorDataReceived += ErrorHandler; process.OutputDataReceived += OutputHandler; void ErrorHandler(object sender, DataReceivedEventArgs e) { if (null != e.Data) { error += e.Data; } } void OutputHandler(object sender, DataReceivedEventArgs e) { if (null != e.Data) { output += e.Data; } } try { //token.ThrowIfCancellationRequested(); if (false == process.Start()) { throw new Exception("exception: unable to start eslint process"); } process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.WaitForExit(); if (false == string.IsNullOrEmpty(error)) { throw new Exception(error); } } catch (OperationCanceledException) { } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } finally { process.Close(); } return output; }, token)); }
public async Task LintAsync(ILinterProvider provider, string filePath, CancellationToken token) { try { await _mutex.WaitAsync(token).ConfigureAwait(false); try { token.ThrowIfCancellationRequested(); var relativePath = Path.GetDirectoryName(filePath) ?? throw new Exception($"exception: could not get directory for file {filePath}"); var executable = EslintHelper.GetExecutableInfo(relativePath); var config = EslintHelper.GetConfigInfo(relativePath); var ignore = EslintHelper.GetIgnoreInfo(relativePath); var arguments = GetArguments(filePath, config?.FullName, ignore?.FullName); var output = await RunAsync(config?.DirectoryName, executable.FullName, arguments, token) .ConfigureAwait(false); token.ThrowIfCancellationRequested(); if (string.IsNullOrEmpty(output)) { throw new Exception("linter returned empty result~ please read output for detailed information ^"); } IEnumerable <EslintResult> results = new List <EslintResult>(); try { results = JsonConvert.DeserializeObject <IEnumerable <EslintResult> >(output); } catch (Exception e) { OutputWindowHelper.WriteLine( "exception: error trying to deserialize output:" + Environment.NewLine + output); OutputWindowHelper.WriteLine(e.Message); } var messages = ProcessResults(results); token.ThrowIfCancellationRequested(); provider.Accept(filePath, messages); } catch (OperationCanceledException) { } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } finally { _mutex.Release(); } } catch (OperationCanceledException) { } catch (Exception e) { OutputWindowHelper.WriteLine(e.Message); } }