Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do ordinal string comparisons #1193

Merged
merged 1 commit into from
Nov 6, 2023
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
26 changes: 17 additions & 9 deletions src/FsAutoComplete.Core/CodeGeneration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ module CodeGenerationUtils =
let displayName = v.DisplayName

if
(v.IsPropertyGetterMethod && displayName.StartsWith("get_"))
|| (v.IsPropertySetterMethod && displayName.StartsWith("set_"))
(v.IsPropertyGetterMethod
&& displayName.StartsWith("get_", StringComparison.Ordinal))
|| (v.IsPropertySetterMethod
&& displayName.StartsWith("set_", StringComparison.Ordinal))
then
displayName.[4..]
else
Expand All @@ -328,7 +330,7 @@ module CodeGenerationUtils =

(if String.IsNullOrWhiteSpace(args) then
""
elif args.StartsWith("(") then
elif args.StartsWith("(", StringComparison.Ordinal) then
args
elif v.CurriedParameterGroups.Count > 1 && (not verboseMode) then
" " + args
Expand All @@ -345,7 +347,10 @@ module CodeGenerationUtils =
| _, _, ".ctor", _ -> "new" + parArgs
// Properties (skipping arguments)
| _, true, _, name when v.IsPropertyGetterMethod || v.IsPropertySetterMethod ->
if name.StartsWith("get_") || name.StartsWith("set_") then
if
name.StartsWith("get_", StringComparison.Ordinal)
|| name.StartsWith("set_", StringComparison.Ordinal)
then
name.[4..]
else
name
Expand Down Expand Up @@ -576,14 +581,14 @@ module CodeGenerationUtils =
| SynBinding(valData = SynValData(Some mf, _, _); headPat = LongIdentPattern(name, range); trivia = trivia) when
mf.MemberKind = SynMemberKind.PropertyGet
->
if name.StartsWith("get_") then
if name.StartsWith("get_", StringComparison.Ordinal) then
Some(name, range, trivia.LeadingKeyword.Range)
else
Some("get_" + name, range, trivia.LeadingKeyword.Range)
| SynBinding(valData = SynValData(Some mf, _, _); headPat = LongIdentPattern(name, range); trivia = trivia) when
mf.MemberKind = SynMemberKind.PropertySet
->
if name.StartsWith("set_") then
if name.StartsWith("set_", StringComparison.Ordinal) then
Some(name, range, trivia.LeadingKeyword.Range)
else
Some("set_" + name, range, trivia.LeadingKeyword.Range)
Expand All @@ -594,9 +599,12 @@ module CodeGenerationUtils =
let normalizeEventName (m: FSharpMemberOrFunctionOrValue) =
let name = m.DisplayName

if name.StartsWith("add_") then name.[4..]
elif name.StartsWith("remove_") then name.[7..]
else name
if name.StartsWith("add_", StringComparison.Ordinal) then
name.[4..]
elif name.StartsWith("remove_", StringComparison.Ordinal) then
name.[7..]
else
name

/// Ideally this info should be returned in error symbols from FCS.
/// Because it isn't, we implement a crude way of getting member signatures:
Expand Down
9 changes: 6 additions & 3 deletions src/FsAutoComplete.Core/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ module Commands =

if
(e.Kind LookupType.Fuzzy) = EntityKind.Attribute
&& lastIdent.EndsWith "Attribute"
&& lastIdent.EndsWith("Attribute", StringComparison.Ordinal)
then
yield
e.TopRequireQualifiedAccessParent,
Expand Down Expand Up @@ -706,7 +706,7 @@ module Commands =
|> Seq.tryFind (fun l ->
let lineStr = getLine l
// namespace MUST be top level -> no indentation
lineStr.StartsWith "namespace ")
lineStr.StartsWith("namespace ", StringComparison.Ordinal))
|> function
// move to the next line below "namespace"
| Some l -> l.IncLine()
Expand Down Expand Up @@ -1000,7 +1000,10 @@ module Commands =
// -> only check if no backticks
let newBacktickedName = newName |> PrettyNaming.NormalizeIdentifierBackticks

if newBacktickedName.StartsWith "``" && newBacktickedName.EndsWith "``" then
if
newBacktickedName.StartsWith("``", StringComparison.Ordinal)
&& newBacktickedName.EndsWith("``", StringComparison.Ordinal)
then
return newBacktickedName
elif PrettyNaming.IsIdentifierName newName then
return newName
Expand Down
14 changes: 9 additions & 5 deletions src/FsAutoComplete.Core/CompilerServiceInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe
let toReplace, otherOpts =
p.OtherOptions
|> Array.partition (fun opt ->
opt.EndsWith "FSharp.Core.dll"
|| opt.EndsWith "FSharp.Compiler.Interactive.Settings.dll")
opt.EndsWith("FSharp.Core.dll", StringComparison.Ordinal)
|| opt.EndsWith("FSharp.Compiler.Interactive.Settings.dll", StringComparison.Ordinal))

{ p with
OtherOptions = Array.append otherOpts [| $"-r:%s{fsc.FullName}"; $"-r:%s{fsi.FullName}" |] }

let (|StartsWith|_|) (prefix: string) (s: string) =
if s.StartsWith(prefix) then
if s.StartsWith(prefix, StringComparison.Ordinal) then
Some(s.[prefix.Length ..])
else
None
Expand All @@ -102,7 +102,7 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe
"mscorlib" ]
|> List.map (fun p -> p + ".dll")

let containsBadRef (s: string) = badRefs |> List.exists (fun r -> s.EndsWith r)
let containsBadRef (s: string) = badRefs |> List.exists (fun r -> s.EndsWith(r, StringComparison.Ordinal))

fun (projOptions: FSharpProjectOptions) ->
{ projOptions with
Expand All @@ -120,7 +120,11 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe
{ projectOptions with
SourceFiles = files }

let (|Reference|_|) (opt: string) = if opt.StartsWith "-r:" then Some(opt.[3..]) else None
let (|Reference|_|) (opt: string) =
if opt.StartsWith("-r:", StringComparison.Ordinal) then
Some(opt.[3..])
else
None

/// ensures that all file paths are absolute before being sent to the compiler, because compilation of scripts fails with relative paths
let resolveRelativeFilePaths (projectOptions: FSharpProjectOptions) =
Expand Down
22 changes: 14 additions & 8 deletions src/FsAutoComplete.Core/DocumentationFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module DocumentationFormatter =
let t = Regex.Replace(org, """<.*>""", "<")

[ yield formatShowDocumentationLink t xmlDocSig assemblyName
if t.EndsWith "<" then
if t.EndsWith("<", StringComparison.Ordinal) then
yield! renderedGenericArgumentTypes |> Seq.intersperse (", ", 2)

yield formatShowDocumentationLink ">" xmlDocSig assemblyName ]
Expand Down Expand Up @@ -217,7 +217,7 @@ module DocumentationFormatter =
let typeList =
unionCase.Fields
|> Seq.map (fun unionField ->
if unionField.Name.StartsWith "Item" then //TODO: Some better way of detecting default names for the union cases' fields
if unionField.Name.StartsWith("Item", StringComparison.Ordinal) then //TODO: Some better way of detecting default names for the union cases' fields
unionField.FieldType |> format displayContext |> fst

else
Expand All @@ -231,7 +231,7 @@ module DocumentationFormatter =
unionCase.DisplayName

let getFuncSignatureWithIdent displayContext (func: FSharpMemberOrFunctionOrValue) (ident: int) =
let maybeGetter = func.LogicalName.StartsWith "get_"
let maybeGetter = func.LogicalName.StartsWith("get_", StringComparison.Ordinal)
let indent = String.replicate ident " "

let functionName =
Expand All @@ -243,7 +243,7 @@ module DocumentationFormatter =
|> FSharpKeywords.NormalizeIdentifierBackticks
elif func.IsOperatorOrActivePattern then
func.DisplayName
elif func.DisplayName.StartsWith "( " then
elif func.DisplayName.StartsWith("( ", StringComparison.Ordinal) then
FSharpKeywords.NormalizeIdentifierBackticks func.LogicalName
else
FSharpKeywords.NormalizeIdentifierBackticks func.DisplayName
Expand Down Expand Up @@ -416,9 +416,12 @@ module DocumentationFormatter =
"new"
elif func.IsOperatorOrActivePattern then
func.DisplayName
elif func.DisplayName.StartsWith "( " then
elif func.DisplayName.StartsWith("( ", StringComparison.Ordinal) then
FSharpKeywords.NormalizeIdentifierBackticks func.LogicalName
elif func.LogicalName.StartsWith "get_" || func.LogicalName.StartsWith "set_" then
elif
func.LogicalName.StartsWith("get_", StringComparison.Ordinal)
|| func.LogicalName.StartsWith("set_", StringComparison.Ordinal)
then
PrettyNaming.TryChopPropertyName func.DisplayName
|> Option.defaultValue func.DisplayName
else
Expand Down Expand Up @@ -536,7 +539,7 @@ module DocumentationFormatter =
let prefix = if v.IsMutable then "val mutable" else "val"

let name =
(if v.DisplayName.StartsWith "( " then
(if v.DisplayName.StartsWith("( ", StringComparison.Ordinal) then
v.LogicalName
else
v.DisplayName)
Expand Down Expand Up @@ -782,7 +785,10 @@ module DocumentationFormatter =

/// trims the leading 'Microsoft.' from the full name of the symbol
member m.SafeFullName =
if m.FullName.StartsWith "Microsoft." && m.Assembly.SimpleName = "FSharp.Core" then
if
m.FullName.StartsWith("Microsoft.", StringComparison.Ordinal)
&& m.Assembly.SimpleName = "FSharp.Core"
then
m.FullName.Substring "Microsoft.".Length
else
m.FullName
Expand Down
4 changes: 2 additions & 2 deletions src/FsAutoComplete.Core/DotnetNewTemplate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ module DotnetNewTemplate =
let parseTemplateOutput (x: string) =
let xs =
x.Split(Environment.NewLine)
|> Array.skipWhile (fun n -> not (n.StartsWith "Template"))
|> Array.skipWhile (fun n -> not (n.StartsWith("Template", StringComparison.Ordinal)))
|> Array.filter (fun n -> not (n.StartsWith ' ' || String.IsNullOrWhiteSpace n))

let header = xs.[0]
let body = xs.[2..]
let nameLength = header.IndexOf("Short")
let nameLength = header.IndexOf("Short", StringComparison.Ordinal)

let body =
body
Expand Down
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/FCSPatches.fs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ module LanguageVersionShim =
/// <returns>A LanguageVersionShim from the parsed "--langversion:" or defaultLanguageVersion </returns>
let fromFSharpProjectOptions (fpo: FSharpProjectOptions) =
fpo.OtherOptions
|> Array.tryFind (fun x -> x.StartsWith("--langversion:"))
|> Array.tryFind (fun x -> x.StartsWith("--langversion:", StringComparison.Ordinal))
|> Option.map (fun x -> x.Split(":")[1])
|> Option.map (fun x -> LanguageVersionShim(x))
|> Option.defaultWith (fun () -> defaultLanguageVersion.Value)
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/FileSystem.fs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ module Tokenizer =
) : Range voption =
match text[range] with
| Error _ -> ValueNone
| Ok rangeText when rangeText.EndsWith "``" ->
| Ok rangeText when rangeText.EndsWith("``", StringComparison.Ordinal) ->
// find matching opening backticks

// backticks cannot contain linebreaks -- even for Active Pattern:
Expand Down
13 changes: 9 additions & 4 deletions src/FsAutoComplete.Core/InlayHints.fs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ module private ShouldCreate =


[<return: Struct>]
let private (|StartsWith|_|) (v: string) (fullName: string) = if fullName.StartsWith v then ValueSome() else ValueNone
let private (|StartsWith|_|) (v: string) (fullName: string) =
if fullName.StartsWith(v, StringComparison.Ordinal) then
ValueSome()
else
ValueNone
// doesn't differentiate between modules, types, namespaces
// -> is just for documentation in code
[<return: Struct>]
Expand Down Expand Up @@ -206,7 +210,8 @@ module private ShouldCreate =

let inline private isMeaningfulName (p: FSharpParameter) = p.DisplayName.Length > 2

let inline private isOperator (func: FSharpMemberOrFunctionOrValue) = func.CompiledName.StartsWith "op_"
let inline private isOperator (func: FSharpMemberOrFunctionOrValue) =
func.CompiledName.StartsWith("op_", StringComparison.Ordinal)

/// Doesn't consider lower/upper cases:
/// * `areSame "foo" "FOO" = true`
Expand Down Expand Up @@ -303,7 +308,7 @@ module private ShouldCreate =

let inline private doesNotMatchArgumentText (parameterName: string) (userArgumentText: string) =
parameterName <> userArgumentText
&& not (userArgumentText.StartsWith parameterName)
&& not (userArgumentText.StartsWith(parameterName, StringComparison.Ordinal))

let private isParamNamePostfixOfFuncName (func: FSharpMemberOrFunctionOrValue) (paramName: string) =
let funcName = func.DisplayName.AsSpan()
Expand Down Expand Up @@ -374,7 +379,7 @@ type MissingExplicitType with
if x.SpecialRules |> List.contains RemoveOptionFromType then
// Optional parameter:
// `static member F(?a) =` -> `: int`, NOT `: int option`
if typeName.EndsWith " option" then
if typeName.EndsWith(" option", StringComparison.Ordinal) then
typeName.Substring(0, typeName.Length - " option".Length)
else
typeName
Expand Down
5 changes: 3 additions & 2 deletions src/FsAutoComplete.Core/Lexer.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FsAutoComplete

open System
open FSharp.Compiler.Tokenization
open FsAutoComplete.Logging
open FsAutoComplete.Logging.Types
Expand Down Expand Up @@ -43,14 +44,14 @@ module Lexer =

[<return: Struct>]
let (|Define|_|) (a: string) =
if a.StartsWith "--define:" then
if a.StartsWith("--define:", StringComparison.Ordinal) then
ValueSome(a.[9..])
else
ValueNone

[<return: Struct>]
let (|LangVersion|_|) (a: string) =
if a.StartsWith "--langversion:" then
if a.StartsWith("--langversion:", StringComparison.Ordinal) then
ValueSome(a.[14..])
else
ValueNone
Expand Down
4 changes: 3 additions & 1 deletion src/FsAutoComplete.Core/ParseAndCheckResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ type ParseAndCheckResults
| FindDeclFailureReason.Unknown r -> r

return ResultOrString.Error(sprintf "Could not find declaration. %s" elaboration)
| FindDeclResult.DeclFound range when range.FileName.EndsWith(Range.rangeStartup.FileName) ->
| FindDeclResult.DeclFound range when
range.FileName.EndsWith(Range.rangeStartup.FileName, StringComparison.Ordinal)
->
return ResultOrString.Error "Could not find declaration"
| FindDeclResult.DeclFound range when range.FileName = UMX.untag x.FileName ->
// decl in same file
Expand Down
15 changes: 9 additions & 6 deletions src/FsAutoComplete.Core/SignatureFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ module SignatureFormatter =
let typeList =
unionCase.Fields
|> Seq.map (fun unionField ->
if unionField.Name.StartsWith "Item" then //TODO: Some better way of detecting default names for the union cases' fields
if unionField.Name.StartsWith("Item", StringComparison.Ordinal) then //TODO: Some better way of detecting default names for the union cases' fields
formatFSharpType displayContext unionField.FieldType
else
unionField.Name + ":" ++ (formatFSharpType displayContext unionField.FieldType))
Expand All @@ -178,7 +178,7 @@ module SignatureFormatter =
unionCase.DisplayName

let getFuncSignatureWithIdent displayContext (func: FSharpMemberOrFunctionOrValue) (ident: int) =
let maybeGetter = func.LogicalName.StartsWith "get_"
let maybeGetter = func.LogicalName.StartsWith("get_", StringComparison.Ordinal)
let indent = String.replicate ident " "

let functionName =
Expand All @@ -190,7 +190,7 @@ module SignatureFormatter =
|> FSharpKeywords.NormalizeIdentifierBackticks
elif func.IsOperatorOrActivePattern then
$"( {func.DisplayNameCore} )"
elif func.DisplayName.StartsWith "( " then
elif func.DisplayName.StartsWith("( ", StringComparison.Ordinal) then
FSharpKeywords.NormalizeIdentifierBackticks func.LogicalName
else
FSharpKeywords.NormalizeIdentifierBackticks func.DisplayName
Expand Down Expand Up @@ -377,9 +377,12 @@ module SignatureFormatter =
"new"
elif func.IsOperatorOrActivePattern then
func.DisplayName
elif func.DisplayName.StartsWith "( " then
elif func.DisplayName.StartsWith("( ", StringComparison.Ordinal) then
FSharpKeywords.NormalizeIdentifierBackticks func.LogicalName
elif func.LogicalName.StartsWith "get_" || func.LogicalName.StartsWith "set_" then
elif
func.LogicalName.StartsWith("get_", StringComparison.Ordinal)
|| func.LogicalName.StartsWith("set_", StringComparison.Ordinal)
then
PrettyNaming.TryChopPropertyName func.DisplayName
|> Option.defaultValue func.DisplayName
else
Expand Down Expand Up @@ -515,7 +518,7 @@ module SignatureFormatter =
let prefix = if v.IsMutable then "val mutable" else "val"

let name =
(if v.DisplayName.StartsWith "( " then
(if v.DisplayName.StartsWith("( ", StringComparison.Ordinal) then
v.LogicalName
else
v.DisplayName)
Expand Down
6 changes: 5 additions & 1 deletion src/FsAutoComplete.Core/SignatureHelp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ let private getSignatureHelpForMethod

let methods = methodGroup.Methods

do! Option.guard (methods.Length > 0 && not (methodGroup.MethodName.EndsWith("> )")))
do!
Option.guard (
methods.Length > 0
&& not (methodGroup.MethodName.EndsWith("> )", StringComparison.Ordinal))
)

let isStaticArgTip = lines.TryGetChar paramLocations.OpenParenLocation = Some '<'

Expand Down
4 changes: 3 additions & 1 deletion src/FsAutoComplete.Core/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ module ProjInfoExtensions =
type FSharpProjectOptions with

member x.OutputDll =
x.OtherOptions |> Array.find (fun o -> o.StartsWith("-o:")) |> (fun s -> s[3..])
x.OtherOptions
|> Array.find (fun o -> o.StartsWith("-o:", StringComparison.Ordinal))
|> (fun s -> s[3..])

member x.SourceFilesThatThisFileDependsOn(file: string<LocalPath>) =
let untagged = UMX.untag file
Expand Down
Loading