Skip to content

Commit

Permalink
Proper file type checking for extracting symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-Ajaj committed Aug 17, 2020
1 parent 48b0151 commit b3e1739
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 104 deletions.
4 changes: 2 additions & 2 deletions NpgsqlFSharpAnalyzer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
RELEASE_NOTES.md = RELEASE_NOTES.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FSharpLintVs", "src\AnalyzerVs\FSharpLintVs.csproj", "{37577282-1289-40DB-AD3D-24499BD09DAE}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NpgsqlFSharpVs", "src\AnalyzerVs\NpgsqlFSharpVs.csproj", "{37577282-1289-40DB-AD3D-24499BD09DAE}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NpgsqlFSharpAnalyzer.Core", "src\NpgsqlFSharpAnalyzer.Core\NpgsqlFSharpAnalyzer.Core.fsproj", "{5964BB56-97B8-4FAE-9933-8113DB11438D}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "NpgsqlFSharpAnalyzer.Core", "src\NpgsqlFSharpAnalyzer.Core\NpgsqlFSharpAnalyzer.Core.fsproj", "{5964BB56-97B8-4FAE-9933-8113DB11438D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/ContentTypeNames.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public static class ContentTypeNames
{
Expand Down
4 changes: 2 additions & 2 deletions src/AnalyzerVs/FsLintVsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
// DO NOT REMOVE THIS MAGICAL INCANTATION NO MATTER HOW MUCH VS WARNS YOU OF DEPRECATION
// --------------------------------------------------------------------------------------
[InstalledProductRegistration("F# Lint", "", "0.1", IconResourceID = 400)]
[InstalledProductRegistration("F# Npgsql Analyzer", "Static SQL analyzer with Npgsql.FSharp", "0.1", IconResourceID = 400)]
// --------------------------------------------------------------------------------------

// Package registration attributes
Expand Down
81 changes: 47 additions & 34 deletions src/AnalyzerVs/LintChecker.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
using Dotnet.ProjInfo.Workspace;
using FSharp.Compiler;
using FSharp.Compiler.SourceCodeServices;
using FSharp.Compiler;
using FSharp.Compiler.Text;
using Npgsql.FSharp.Analyzers.Core;
using Microsoft.FSharp.Control;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Language.StandardClassification;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Threading;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.FSharp.Core;
using Microsoft.FSharp.Collections;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
///<summary>
/// Finds the linting errors in comments for a specific buffer.
Expand Down Expand Up @@ -171,12 +163,13 @@ private void RunLinter()
public async Task DoUpdateAsync()
{
if (IsDisposed)
{
return;
}

var buffer = _currentSnapshot;
var path = _document.FilePath;


// replace with user token
var token = _cts.Token;
var instance = await FsLintVsPackage.Instance.WithCancellation(token);
Expand Down Expand Up @@ -210,53 +203,73 @@ public async Task DoUpdateAsync()
return;
}

var defaults = FSharpParsingOptions.Default;
var parseOpts = new FSharpParsingOptions(
sourceFiles: new string[] { path },
conditionalCompilationDefines: defaults.ConditionalCompilationDefines,
errorSeverityOptions: defaults.ErrorSeverityOptions,
isInteractive: defaults.IsInteractive,
lightSyntax: defaults.LightSyntax,
compilingFsLib: defaults.CompilingFsLib,
isExe: defaults.IsExe
);
var loadedSchema = SqlAnalysis.databaseSchema(connectionString);

if (loadedSchema.IsError)
{
return;
}

var source = _currentSnapshot.GetText();
var sourceText = SourceText.ofString(source);
var parseAsync = _provider.CheckerInstance.ParseFile(path, sourceText, parseOpts, null);
var parseResults = await FSharpAsync.StartAsTask(parseAsync, null, token);
if (parseResults.ParseHadErrors)

var getProjectOptions = _provider.CheckerInstance.GetProjectOptionsFromScript(
filename: path,
sourceText: sourceText,
assumeDotNetFramework: false,
useSdkRefs: true,
useFsiAuxLib: true,
previewEnabled: true,
otherFlags: new string[] { "--targetprofile:netstandard" },
loadedTimeStamp: FSharpOption<DateTime>.None,
extraProjectInfo: FSharpOption<object>.None,
optionsStamp: FSharpOption<long>.None,
userOpName: FSharpOption<string>.None
);

var (options, errors) = await FSharpAsync.StartAsTask(getProjectOptions, null, token);

if (errors.Any())
{
return;
}

var performParseAndCheck = _provider.CheckerInstance.ParseAndCheckFileInProject(
filename: path,
fileversion: 1,
sourceText: sourceText,
options: options,
textSnapshotInfo: FSharpOption<object>.None,
userOpName: FSharpOption<string>.None
);

var loadedSchema = SqlAnalysis.databaseSchema(connectionString);
var (parseResults, checkAnswer) = await FSharpAsync.StartAsTask(performParseAndCheck, null, token);

if (loadedSchema.IsError)
if (parseResults.ParseHadErrors || checkAnswer.IsAborted)
{
return;
}

var context = new SpecializedContext(
var checkResults = SqlAnalyzer.checkAnswerResult(checkAnswer).Value;

var context = new SqlAnalyzerContext(
fileName: path,
content: source.Split('\n'),
parseTree: parseResults.ParseTree.Value,
symbols: FSharpList<FSharpEntity>.Empty
symbols: SqlAnalyzer.getSymbols(checkResults)
);

var operations = SyntacticAnalysis.findSqlOperations(context).ToList();
var databaseSchema = loadedSchema.ResultValue;

var foundErrors =
from operation in operations
from message in SqlAnalysis.analyzeOperation(operation, connectionString, databaseSchema)
select message;
var errorMessages =
from operation in SyntacticAnalysis.findSqlOperations(context)
from analysisOutput in SqlAnalysis.analyzeOperation(operation, connectionString, databaseSchema)
select analysisOutput;

var oldLintingErrors = this.Factory.CurrentSnapshot;
var newLintErrors = new LintingErrorsSnapshot(_document, oldLintingErrors.VersionNumber + 1);

foreach (var error in foundErrors)
foreach (var error in errorMessages)
{
var span = RangeToSpan(error.Range, buffer);
newLintErrors.Errors.Add(new LintError(span, error, Project));
Expand Down
4 changes: 2 additions & 2 deletions src/AnalyzerVs/LintCheckerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FSharp.Compiler.SourceCodeServices;
using FSharpLintVs;
using NpgsqlFSharpVs;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell.TableControl;
using Microsoft.VisualStudio.Shell.TableManager;
Expand All @@ -14,7 +14,7 @@
using System.ComponentModel.Composition;
using System.Diagnostics;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
/// <summary>
/// Factory for the <see cref="ITagger{T}"/> and <see cref="ITableDataSource"/>.
Expand Down
7 changes: 2 additions & 5 deletions src/AnalyzerVs/LintError.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Npgsql.FSharp.Analyzers.Core;
using Microsoft.FSharp.Core;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Utilities;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintError
{
Expand All @@ -16,7 +13,7 @@ public class LintError

public int NextIndex = -1;

public string Tooltip => $"{LintWarning.Code}: {LintWarning.Message}";
public string Tooltip => LintWarning.Message;

public string Identifier => LintWarning.Type;

Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/LintProjectInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.VisualStudio.Shell.Interop;
using System;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintProjectInfo
{
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/LintTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Collections.Generic;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintTagger : ITagger<IErrorTag>, IDisposable
{
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Options/FsLintOptionsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.VisualStudio.Shell;
using System.Runtime.InteropServices;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
[Guid(GuidString)]
public class FsLintOptionsPage : DialogPage
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/SubscriptionManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.VisualStudio.Shell.TableManager;
using System;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
/// <summary>
/// Every consumer of data from an <see cref="ITableDataSource"/> provides an <see cref="ITableDataSink"/> to record the changes. We give the consumer
Expand Down
4 changes: 2 additions & 2 deletions src/AnalyzerVs/SuggestionPreview.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<UserControl x:Class="FSharpLintVs.SuggestionPreview"
<UserControl x:Class="NpgsqlFSharpVs.SuggestionPreview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
xmlns:local="clr-namespace:FSharpLintVs"
xmlns:local="clr-namespace:NpgsqlFSharpVs"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="400"
>
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/SuggestionPreview.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
/// <summary>
/// Interaction logic for SuggestionPreview.xaml
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Suggestions/LintActionsSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintActionsSource : ISuggestedActionsSource
{
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Suggestions/LintFixAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.VisualStudio.Text;
using Npgsql.FSharp.Analyzers.Core;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintFixAction : ISuggestedAction
{
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Suggestions/LintSuggestionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.VisualStudio.Utilities;
using System.ComponentModel.Composition;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
[Export(typeof(ISuggestedActionsSourceProvider))]
[ContentType(ContentTypeNames.FSharpContentType)]
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Suggestions/LintSuppressAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Threading.Tasks;
using System.Windows;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintSuppressAction : ISuggestedAction
{
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Suggestions/LintSuppressBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading.Tasks;
using System.Windows;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintSuppressBy : ISuggestedAction
{
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Table/LintTableSnapshotFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.Shell.TableManager;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintTableSnapshotFactory : TableEntriesSnapshotFactoryBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyzerVs/Table/LintingErrorsSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Windows;
using System.Windows.Documents;

namespace FSharpLintVs
namespace NpgsqlFSharpVs
{
public class LintingErrorsSnapshot : WpfTableEntriesSnapshotBase
{
Expand Down
Loading

0 comments on commit b3e1739

Please sign in to comment.