提交 13783e2e 编写于 作者: A Andrew Hall (METAL)

Correct validation of namespace identifier

上级 d25e9b81
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Globalization;
using System.Linq;
namespace Roslyn.Utilities
{
......@@ -109,6 +110,19 @@ public static bool IsValidIdentifier(string name)
return true;
}
/// <summary>
/// Returns true if every identifier in a namespace is valid using <see cref="IsValidIdentifier(string)"/>
/// </summary>
public static bool IsValidNamespace(string @namespace)
{
if (string.IsNullOrEmpty(@namespace))
{
return false;
}
return @namespace.Split('.').All(IsValidIdentifier);
}
private static bool IsLetterChar(UnicodeCategory cat)
{
// letter-character:
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
extern alias WORKSPACES;
using System;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
......@@ -25,7 +24,7 @@ public TestMoveToNamespaceOptionsService()
{
}
public Task<MoveToNamespaceOptionsResult> GetChangeNamespaceOptionsAsync(ISyntaxFactsService syntaxFactsService, INotificationService notificationService, string defaultNamespace, CancellationToken cancellationToken)
public Task<MoveToNamespaceOptionsResult> GetChangeNamespaceOptionsAsync(string defaultNamespace, ImmutableArray<string> availableNamespaces, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ChangeNamespace;
......@@ -125,8 +127,6 @@ private bool ContainsNamespaceDeclaration(SyntaxNode node)
.Select(n => n.ToDisplayString(QualifiedNamespaceFormat));
return await _moveToNamespaceOptionsService.GetChangeNamespaceOptionsAsync(
syntaxFactsService,
notificationService,
defaultNamespace,
namespaces.ToImmutableArray(),
cancellationToken).ConfigureAwait(false);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Notification;
namespace Microsoft.CodeAnalysis.MoveToNamespace
{
internal interface IMoveToNamespaceOptionsService : IWorkspaceService
{
Task<MoveToNamespaceOptionsResult> GetChangeNamespaceOptionsAsync(
ISyntaxFactsService syntaxFactsService,
INotificationService notificationService,
string defaultNamespace,
ImmutableArray<string> availableNamespaces,
CancellationToken cancellationToken);
......
......@@ -5,13 +5,14 @@
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.LanguageServices.Implementation.Utilities;
using Microsoft.VisualStudio.Imaging;
using Microsoft.CodeAnalysis.MoveToNamespace;
using Roslyn.Utilities;
namespace Microsoft.VisualStudio.LanguageServices.Implementation.MoveToNamespace
{
class MoveToNamespaceDialogViewModel : AbstractNotifyPropertyChanged
{
public MoveToNamespaceDialogViewModel(
IGlyphService glyphService,
string defaultNamespace,
ImmutableArray<string> availableNamespaces)
{
......@@ -34,7 +35,7 @@ private void MoveToNamespaceDialogViewModel_PropertyChanged(object sender, Syste
public void OnNamespaceUpdated()
{
var isNewNamespace = !AvailableNamespaces.Contains(NamespaceName);
var isValidName = !isNewNamespace || IsValidNamespace(NamespaceName);
var isValidName = !isNewNamespace || UnicodeCharacterUtilities.IsValidNamespace(NamespaceName);
if (isNewNamespace && isValidName)
{
......@@ -57,11 +58,6 @@ public void OnNamespaceUpdated()
}
}
private static bool IsValidNamespace(string @namespace)
{
return !@namespace.Contains(" ");
}
private string _namespaceName;
public string NamespaceName
{
......
......@@ -29,8 +29,6 @@ public VisualStudioMoveToNamespaceOptionsService(IGlyphService glyphService, ITh
}
public async Task<MoveToNamespaceOptionsResult> GetChangeNamespaceOptionsAsync(
ISyntaxFactsService syntaxFactsService,
INotificationService notificationService,
string defaultNamespace,
ImmutableArray<string> availableNamespaces,
CancellationToken cancellationToken)
......@@ -39,7 +37,6 @@ public VisualStudioMoveToNamespaceOptionsService(IGlyphService glyphService, ITh
cancellationToken.ThrowIfCancellationRequested();
var viewModel = new MoveToNamespaceDialogViewModel(
_glyphService,
defaultNamespace,
availableNamespaces);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册