b1c59844ac1ebaae211e1f30ee26b252f2b9a962dc6e397344f6e97159233d0bef3e7b41f0113aaaaf6df79cd87eec904a7fccae25a26ed1a7a75b3d1611a5 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { match as matchGlobPattern } from '../../base/common/glob.js';
  6. import { normalize } from '../../base/common/path.js';
  7. export function score(selector, candidateUri, candidateLanguage, candidateIsSynchronized, candidateNotebookUri, candidateNotebookType) {
  8. if (Array.isArray(selector)) {
  9. // array -> take max individual value
  10. let ret = 0;
  11. for (const filter of selector) {
  12. const value = score(filter, candidateUri, candidateLanguage, candidateIsSynchronized, candidateNotebookUri, candidateNotebookType);
  13. if (value === 10) {
  14. return value; // already at the highest
  15. }
  16. if (value > ret) {
  17. ret = value;
  18. }
  19. }
  20. return ret;
  21. }
  22. else if (typeof selector === 'string') {
  23. if (!candidateIsSynchronized) {
  24. return 0;
  25. }
  26. // short-hand notion, desugars to
  27. // 'fooLang' -> { language: 'fooLang'}
  28. // '*' -> { language: '*' }
  29. if (selector === '*') {
  30. return 5;
  31. }
  32. else if (selector === candidateLanguage) {
  33. return 10;
  34. }
  35. else {
  36. return 0;
  37. }
  38. }
  39. else if (selector) {
  40. // filter -> select accordingly, use defaults for scheme
  41. const { language, pattern, scheme, hasAccessToAllModels, notebookType } = selector; // TODO: microsoft/TypeScript#42768
  42. if (!candidateIsSynchronized && !hasAccessToAllModels) {
  43. return 0;
  44. }
  45. // selector targets a notebook -> use the notebook uri instead
  46. // of the "normal" document uri.
  47. if (notebookType && candidateNotebookUri) {
  48. candidateUri = candidateNotebookUri;
  49. }
  50. let ret = 0;
  51. if (scheme) {
  52. if (scheme === candidateUri.scheme) {
  53. ret = 10;
  54. }
  55. else if (scheme === '*') {
  56. ret = 5;
  57. }
  58. else {
  59. return 0;
  60. }
  61. }
  62. if (language) {
  63. if (language === candidateLanguage) {
  64. ret = 10;
  65. }
  66. else if (language === '*') {
  67. ret = Math.max(ret, 5);
  68. }
  69. else {
  70. return 0;
  71. }
  72. }
  73. if (notebookType) {
  74. if (notebookType === candidateNotebookType) {
  75. ret = 10;
  76. }
  77. else if (notebookType === '*' && candidateNotebookType !== undefined) {
  78. ret = Math.max(ret, 5);
  79. }
  80. else {
  81. return 0;
  82. }
  83. }
  84. if (pattern) {
  85. let normalizedPattern;
  86. if (typeof pattern === 'string') {
  87. normalizedPattern = pattern;
  88. }
  89. else {
  90. // Since this pattern has a `base` property, we need
  91. // to normalize this path first before passing it on
  92. // because we will compare it against `Uri.fsPath`
  93. // which uses platform specific separators.
  94. // Refs: https://github.com/microsoft/vscode/issues/99938
  95. normalizedPattern = Object.assign(Object.assign({}, pattern), { base: normalize(pattern.base) });
  96. }
  97. if (normalizedPattern === candidateUri.fsPath || matchGlobPattern(normalizedPattern, candidateUri.fsPath)) {
  98. ret = 10;
  99. }
  100. else {
  101. return 0;
  102. }
  103. }
  104. return ret;
  105. }
  106. else {
  107. return 0;
  108. }
  109. }