Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Analysis/Impl/Infrastructure/Check.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Diagnostics;
using static System.FormattableString;

namespace Microsoft.PythonTools.Analysis.Infrastructure {
public static class Check {
[DebuggerStepThrough]
public static void ArgumentNull(string argumentName, object argument) {
if (argument == null) {
throw new ArgumentNullException(argumentName);
}
}

[DebuggerStepThrough]
public static void ArgumentStringNullOrEmpty(string argumentName, string argument) {
Check.ArgumentNull(argumentName, argument);

if (string.IsNullOrEmpty(argument)) {
throw new ArgumentException(argumentName);
}
}

[DebuggerStepThrough]
public static void ArgumentOutOfRange(string argumentName, Func<bool> predicate) {
if (predicate()) {
throw new ArgumentOutOfRangeException(argumentName);
}
}

[DebuggerStepThrough]
public static void InvalidOperation(Func<bool> predicate, string message = null) {
if (!predicate()) {
throw new InvalidOperationException(message ?? string.Empty);
}
}

[DebuggerStepThrough]
public static void Argument(string argumentName, Func<bool> predicate) {
if (!predicate()) {
throw new ArgumentException(Invariant($"{argumentName} is not valid"));
}
}
}
}
34 changes: 34 additions & 0 deletions src/Analysis/Impl/Infrastructure/Definitions/IDocumentReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using Microsoft.PythonTools.Analysis.LanguageServer;
using Microsoft.PythonTools.Parsing.Ast;

namespace Microsoft.PythonTools.Analysis.Infrastructure {
public interface IDocumentReader {
string ReadToEnd();
string Read(int start, int count);
}

public static class DocumentReaderExtensions {
public static string ReadLinearSpan(this IDocumentReader reader, LinearSpan span)
=> reader.Read(span.Start, span.Length);
public static string ReadRange(this IDocumentReader reader, Range range, PythonAst ast)
=> reader.ReadLinearSpan(range.ToLinearSpan(ast));
public static string ReadSourceSpan(this IDocumentReader reader, SourceSpan span, PythonAst ast)
=> reader.ReadLinearSpan(span.ToLinearSpan(ast));
}
}
52 changes: 52 additions & 0 deletions src/Analysis/Impl/Infrastructure/DocumentReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using Microsoft.PythonTools.Analysis.LanguageServer;
using Microsoft.PythonTools.Parsing.Ast;

namespace Microsoft.PythonTools.Analysis.Infrastructure {
public sealed class DocumentReader : IDocumentReader {
private readonly IDocument _doc;
private readonly int _part;
private string _content;
private bool _read;

public DocumentReader(IDocument doc, int part) {
_doc = doc;
_part = part;
}

public string ReadToEnd() => Content;

public string Read(int start, int count) => Content?.Substring(start, count);

private string Content {
get {
if (_content == null && !_read) {
var reader = _doc.ReadDocument(_part, out _);
_read = true;
if (reader == null) {
return null;
}
using (reader) {
_content = reader.ReadToEnd();
}
}
return _content;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using Microsoft.PythonTools.Analysis.LanguageServer;
using Microsoft.PythonTools.Parsing.Ast;

namespace Microsoft.PythonTools.Analysis.Infrastructure {
public static class SourceLocationExtensions {
public static int ToIndex(this SourceLocation location, PythonAst ast) => ast.LocationToIndex(location);
}
public static class SourceSpanExtensions {
public static LinearSpan ToLinearSpan(this SourceSpan span, PythonAst ast)
=> LinearSpan.FromBounds(ast.LocationToIndex(span.Start), ast.LocationToIndex(span.End));
public static LinearSpan ToLinearSpan(this Range range, PythonAst ast)
=> LinearSpan.FromBounds(ast.LocationToIndex(range.start), ast.LocationToIndex(range.end));
}
}
Loading