Skip to content

Commit c005ef7

Browse files
Switch to better document selecting (#1218)
1 parent 9e3f518 commit c005ef7

13 files changed

+50
-88
lines changed

src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Extensions.Logging;
1212
using Microsoft.PowerShell.EditorServices.Services;
1313
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
14+
using Microsoft.PowerShell.EditorServices.Utility;
1415
using Newtonsoft.Json.Linq;
1516
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1617
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
@@ -42,7 +43,7 @@ public CodeActionHandler(ILoggerFactory factory, AnalysisService analysisService
4243
_workspaceService = workspaceService;
4344
_registrationOptions = new CodeActionRegistrationOptions
4445
{
45-
DocumentSelector = new DocumentSelector(new DocumentFilter() { Language = "powershell" }),
46+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
4647
CodeActionKinds = s_supportedCodeActions
4748
};
4849
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeLensHandlers.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.PowerShell.EditorServices.Logging;
1515
using Microsoft.PowerShell.EditorServices.Services;
1616
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
17+
using Microsoft.PowerShell.EditorServices.Utility;
1718
using OmniSharp.Extensions.LanguageServer.Protocol;
1819
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1920
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
@@ -23,13 +24,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
2324
{
2425
internal class CodeLensHandlers : ICodeLensHandler, ICodeLensResolveHandler
2526
{
26-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
27-
new DocumentFilter
28-
{
29-
Language = "powershell"
30-
}
31-
);
32-
3327
private readonly ILogger _logger;
3428
private readonly SymbolsService _symbolsService;
3529
private readonly WorkspaceService _workspaceService;
@@ -47,7 +41,7 @@ CodeLensRegistrationOptions IRegistration<CodeLensRegistrationOptions>.GetRegist
4741
{
4842
return new CodeLensRegistrationOptions
4943
{
50-
DocumentSelector = _documentSelector,
44+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
5145
ResolveProvider = true
5246
};
5347
}
@@ -65,7 +59,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
6559
{
6660
return new TextDocumentRegistrationOptions
6761
{
68-
DocumentSelector = _documentSelector,
62+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
6963
};
7064
}
7165

src/PowerShellEditorServices/Services/TextDocument/Handlers/CompletionHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CompletionRegistrationOptions GetRegistrationOptions()
5454
{
5555
return new CompletionRegistrationOptions
5656
{
57-
DocumentSelector = new DocumentSelector(new DocumentFilter { Language = "powershell" }),
57+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
5858
ResolveProvider = true,
5959
TriggerCharacters = new[] { ".", "-", ":", "\\" }
6060
};

src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1919
{
2020
internal class DefinitionHandler : IDefinitionHandler
2121
{
22-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
23-
new DocumentFilter
24-
{
25-
Language = "powershell"
26-
}
27-
);
28-
2922
private readonly ILogger _logger;
3023
private readonly SymbolsService _symbolsService;
3124
private readonly WorkspaceService _workspaceService;
@@ -46,7 +39,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
4639
{
4740
return new TextDocumentRegistrationOptions
4841
{
49-
DocumentSelector = _documentSelector
42+
DocumentSelector = LspUtils.PowerShellDocumentSelector
5043
};
5144
}
5245

src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public DocumentHighlightHandler(
4141
_symbolsService = symbolService;
4242
_registrationOptions = new TextDocumentRegistrationOptions()
4343
{
44-
DocumentSelector = new DocumentSelector(new DocumentFilter() { Language = "powershell" } )
44+
DocumentSelector = LspUtils.PowerShellDocumentSelector
4545
};
4646
_logger.LogInformation("highlight handler loaded");
4747
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
2424
{
2525
internal class DocumentSymbolHandler : IDocumentSymbolHandler
2626
{
27-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
28-
new DocumentFilter
29-
{
30-
Language = "powershell"
31-
}
32-
);
33-
3427
private readonly ILogger _logger;
3528
private readonly WorkspaceService _workspaceService;
3629

@@ -54,7 +47,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
5447
{
5548
return new TextDocumentRegistrationOptions
5649
{
57-
DocumentSelector = _documentSelector,
50+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
5851
};
5952
}
6053

src/PowerShellEditorServices/Services/TextDocument/Handlers/FoldingRangeHandler.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Logging;
1010
using Microsoft.PowerShell.EditorServices.Services;
1111
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
12+
using Microsoft.PowerShell.EditorServices.Utility;
1213
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1314
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1415
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
@@ -17,13 +18,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1718
{
1819
internal class FoldingRangeHandler : IFoldingRangeHandler
1920
{
20-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
21-
new DocumentFilter()
22-
{
23-
Language = "powershell"
24-
}
25-
);
26-
2721
private readonly ILogger _logger;
2822
private readonly ConfigurationService _configurationService;
2923
private readonly WorkspaceService _workspaceService;
@@ -40,7 +34,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
4034
{
4135
return new TextDocumentRegistrationOptions()
4236
{
43-
DocumentSelector = _documentSelector,
37+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
4438
};
4539
}
4640

src/PowerShellEditorServices/Services/TextDocument/Handlers/FormattingHandlers.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Microsoft.Extensions.Logging;
99
using Microsoft.PowerShell.EditorServices.Services;
10+
using Microsoft.PowerShell.EditorServices.Utility;
1011
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1112
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1213
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
@@ -15,13 +16,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1516
{
1617
internal class DocumentFormattingHandler : IDocumentFormattingHandler
1718
{
18-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
19-
new DocumentFilter()
20-
{
21-
Language = "powershell"
22-
}
23-
);
24-
2519
private readonly ILogger _logger;
2620
private readonly AnalysisService _analysisService;
2721
private readonly ConfigurationService _configurationService;
@@ -40,7 +34,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
4034
{
4135
return new TextDocumentRegistrationOptions
4236
{
43-
DocumentSelector = _documentSelector
37+
DocumentSelector = LspUtils.PowerShellDocumentSelector
4438
};
4539
}
4640

@@ -93,13 +87,6 @@ public void SetCapability(DocumentFormattingCapability capability)
9387

9488
internal class DocumentRangeFormattingHandler : IDocumentRangeFormattingHandler
9589
{
96-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
97-
new DocumentFilter()
98-
{
99-
Pattern = "**/*.ps*1"
100-
}
101-
);
102-
10390
private readonly ILogger _logger;
10491
private readonly AnalysisService _analysisService;
10592
private readonly ConfigurationService _configurationService;
@@ -118,7 +105,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
118105
{
119106
return new TextDocumentRegistrationOptions
120107
{
121-
DocumentSelector = _documentSelector
108+
DocumentSelector = LspUtils.PowerShellDocumentSelector
122109
};
123110
}
124111

src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.PowerShell.EditorServices.Services;
1111
using Microsoft.PowerShell.EditorServices.Services.Symbols;
1212
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
13+
using Microsoft.PowerShell.EditorServices.Utility;
1314
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1415
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1516
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
@@ -18,13 +19,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1819
{
1920
internal class HoverHandler : IHoverHandler
2021
{
21-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
22-
new DocumentFilter
23-
{
24-
Language = "powershell"
25-
}
26-
);
27-
2822
private readonly ILogger _logger;
2923
private readonly SymbolsService _symbolsService;
3024
private readonly WorkspaceService _workspaceService;
@@ -48,7 +42,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
4842
{
4943
return new TextDocumentRegistrationOptions
5044
{
51-
DocumentSelector = _documentSelector,
45+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
5246
};
5347
}
5448

src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1919
{
2020
class ReferencesHandler : IReferencesHandler
2121
{
22-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
23-
new DocumentFilter()
24-
{
25-
Language = "powershell"
26-
}
27-
);
28-
2922
private readonly ILogger _logger;
3023
private readonly SymbolsService _symbolsService;
3124
private readonly WorkspaceService _workspaceService;
@@ -42,7 +35,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
4235
{
4336
return new TextDocumentRegistrationOptions
4437
{
45-
DocumentSelector = _documentSelector
38+
DocumentSelector = LspUtils.PowerShellDocumentSelector
4639
};
4740
}
4841

src/PowerShellEditorServices/Services/TextDocument/Handlers/SignatureHelpHandler.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.PowerShell.EditorServices.Services;
1212
using Microsoft.PowerShell.EditorServices.Services.Symbols;
1313
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
14+
using Microsoft.PowerShell.EditorServices.Utility;
1415
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1516
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1617
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
@@ -20,14 +21,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
2021
internal class SignatureHelpHandler : ISignatureHelpHandler
2122
{
2223
private static readonly SignatureInformation[] s_emptySignatureResult = Array.Empty<SignatureInformation>();
23-
24-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
25-
new DocumentFilter()
26-
{
27-
Language = "powershell"
28-
}
29-
);
30-
3124
private readonly ILogger _logger;
3225
private readonly SymbolsService _symbolsService;
3326
private readonly WorkspaceService _workspaceService;
@@ -51,7 +44,7 @@ public SignatureHelpRegistrationOptions GetRegistrationOptions()
5144
{
5245
return new SignatureHelpRegistrationOptions
5346
{
54-
DocumentSelector = _documentSelector,
47+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
5548
// A sane default of " ". We may be able to include others like "-".
5649
TriggerCharacters = new Container<string>(" ")
5750
};

src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1616
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
1717
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
18+
using Microsoft.PowerShell.EditorServices.Utility;
1819

1920
namespace Microsoft.PowerShell.EditorServices.Handlers
2021
{
@@ -25,14 +26,6 @@ class TextDocumentHandler : ITextDocumentSyncHandler
2526
private readonly AnalysisService _analysisService;
2627
private readonly WorkspaceService _workspaceService;
2728
private readonly RemoteFileManagerService _remoteFileManagerService;
28-
29-
private readonly DocumentSelector _documentSelector = new DocumentSelector(
30-
new DocumentFilter()
31-
{
32-
Language = "powershell"
33-
}
34-
);
35-
3629
private SynchronizationCapability _capability;
3730

3831
public TextDocumentSyncKind Change => TextDocumentSyncKind.Incremental;
@@ -72,7 +65,7 @@ TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrati
7265
{
7366
return new TextDocumentChangeRegistrationOptions()
7467
{
75-
DocumentSelector = _documentSelector,
68+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
7669
SyncKind = Change
7770
};
7871
}
@@ -101,7 +94,7 @@ TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.G
10194
{
10295
return new TextDocumentRegistrationOptions()
10396
{
104-
DocumentSelector = _documentSelector,
97+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
10598
};
10699
}
107100

@@ -138,7 +131,7 @@ TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOp
138131
{
139132
return new TextDocumentSaveRegistrationOptions()
140133
{
141-
DocumentSelector = _documentSelector,
134+
DocumentSelector = LspUtils.PowerShellDocumentSelector,
142135
IncludeText = true
143136
};
144137
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Utility
9+
{
10+
internal static class LspUtils
11+
{
12+
public static DocumentSelector PowerShellDocumentSelector => new DocumentSelector(
13+
DocumentFilter.ForLanguage("powershell"),
14+
DocumentFilter.ForLanguage("pwsh"),
15+
16+
// The vim extension sets all PowerShell files as language "ps1" so this
17+
// makes sure we track those.
18+
DocumentFilter.ForLanguage("ps1"),
19+
DocumentFilter.ForLanguage("psm1"),
20+
DocumentFilter.ForLanguage("psd1"),
21+
22+
// Also specify the file extensions to be thorough
23+
// This won't handle untitled files which is why we have to do the ones above.
24+
DocumentFilter.ForPattern("**/*.ps*1")
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)