dsh-gufa-code 1.3.82 → 1.3.88

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -400,7 +400,7 @@
400
400
  }
401
401
 
402
402
  /** 这些声明类型算「定义级」,多个同名时值得让用户选;普通 const/赋值等弱匹配不轻易弹列表。 */
403
- const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'def', 'func', 'fn', 'type', 'export'])
403
+ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'def', 'func', 'fn', 'type', 'export', 'lsp'])
404
404
 
405
405
  /* ── model / URI ────────────────────────────────────────────────────── */
406
406
 
@@ -682,12 +682,21 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
682
682
  if (host === null) return undefined
683
683
  let cancelled = false
684
684
  let editor = null
685
+ let definitionCleanup = null
685
686
  const subscriptions = []
686
687
 
687
688
  setStatus('loading')
688
689
  setMonacoError('')
689
690
 
690
691
  const disposeEditor = () => {
692
+ if (definitionCleanup !== null) {
693
+ try {
694
+ definitionCleanup()
695
+ } catch {
696
+ /* ignore */
697
+ }
698
+ definitionCleanup = null
699
+ }
691
700
  for (const subscription of subscriptions.splice(0)) {
692
701
  try {
693
702
  subscription.dispose()
@@ -750,11 +759,274 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
750
759
  }
751
760
  })
752
761
 
753
- /* Cmd/Ctrl+点击跳转定义:先走 Monaco 语言服务;没有提供者(未打开的文件、
754
- 非 TS/JS 语言等)时回退到 Host 的项目级定义搜索,单结果直接跳,多结果弹选择。 */
762
+ /* Cmd/Ctrl+点击跳转定义 + Cmd 悬停「可点击」提示:先走 Monaco 语言服务;
763
+ 没有提供者(未打开的文件、非 TS/JS 语言等)时回退到 Host 的项目级定义搜索。 */
755
764
  try {
756
- const domNode = typeof instance.getDomNode === 'function' ? instance.getDomNode() : null
757
- if (domNode !== null && domNode !== undefined) {
765
+ const editorDom = typeof instance.getDomNode === 'function' ? instance.getDomNode() : null
766
+ if (editorDom !== null && editorDom !== undefined) {
767
+ const isPrimaryEvent = (event) => {
768
+ const mac = typeof G.isMacPlatform === 'function' ? G.isMacPlatform() : true
769
+ return mac ? event.metaKey === true : event.ctrlKey === true
770
+ }
771
+ /* JS/TS:把当前文件的 import 依赖按需加载成 Monaco model,让 TS 语言服务
772
+ 能解析跨文件定义;否则 Monaco 只认识已经打开过的标签。 */
773
+ const tsPreloadState = { seen: new Set() }
774
+ const preloadTsDependencies = async (model, path, root) => {
775
+ const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
776
+ if (language !== 'typescript' && language !== 'javascript') return
777
+ const version = typeof model.getVersionId === 'function' ? model.getVersionId() : 0
778
+ const key = `${path}|${version}`
779
+ if (tsPreloadState.seen.has(key) || tsPreloadState.seen.size > 200) return
780
+ tsPreloadState.seen.add(key)
781
+ let content = ''
782
+ try {
783
+ content = model.getValue()
784
+ } catch {
785
+ return
786
+ }
787
+ const specs = []
788
+ for (const pattern of [/import\s+(?:[^'"]*?\s+from\s+)?['"]([^'"]+)['"]/g, /require\(\s*['"]([^'"]+)['"]\s*\)/g]) {
789
+ let match = null
790
+ while ((match = pattern.exec(content)) !== null) {
791
+ if (typeof match[1] === 'string' && match[1].startsWith('.')) specs.push(match[1])
792
+ }
793
+ }
794
+ const baseDir = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : ''
795
+ const candidates = (spec) => {
796
+ const raw = `${baseDir}${baseDir === '' ? '' : '/'}${spec}`
797
+ const parts = []
798
+ for (const part of raw.split('/')) {
799
+ if (part === '' || part === '.') continue
800
+ if (part === '..') parts.pop()
801
+ else parts.push(part)
802
+ }
803
+ const base = parts.join('/')
804
+ return [base, `${base}.ts`, `${base}.tsx`, `${base}.js`, `${base}.jsx`, `${base}.mjs`, `${base}.cjs`, `${base}.d.ts`, `${base}/index.ts`, `${base}/index.js`, `${base}/index.tsx`]
805
+ }
806
+ for (const spec of [...new Set(specs)].slice(0, 30)) {
807
+ for (const rel of candidates(spec)) {
808
+ try {
809
+ const doc = await G.api('fs.read', { root, path: rel })
810
+ const uri = gufaModelUri(monaco, root, rel)
811
+ if (monaco.editor.getModel(uri) === null) {
812
+ monaco.editor.createModel(String(doc.content ?? ''), gufaResolveLanguage(monaco, doc.language), uri)
813
+ }
814
+ break
815
+ } catch {
816
+ /* 尝试下一个候选 */
817
+ }
818
+ }
819
+ }
820
+ }
821
+ const queryDefinitions = (position, word, model, path, root) => {
822
+ const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
823
+ if (language === 'python') {
824
+ return G.api('lsp.definition', {
825
+ root,
826
+ path,
827
+ line: position.lineNumber,
828
+ column: position.column,
829
+ language: 'python',
830
+ content: model.getValue(),
831
+ }).then((value) => ({
832
+ matches: value !== null && value !== undefined && Array.isArray(value.matches) ? value.matches : [],
833
+ current: value !== null && value !== undefined && value.current !== undefined ? value.current : undefined,
834
+ word,
835
+ }))
836
+ }
837
+ return G.api('code.definition', {
838
+ root,
839
+ path,
840
+ line: position.lineNumber,
841
+ column: position.column,
842
+ word,
843
+ content: model.getValue(),
844
+ })
845
+ }
846
+ const decideDefinition = (result) => {
847
+ const matches = result !== null && result !== undefined && Array.isArray(result.matches) ? result.matches : []
848
+ if (matches.length === 0) return { kind: 'none' }
849
+ const currentPath = result.current !== null && result.current !== undefined && typeof result.current.path === 'string' ? result.current.path : null
850
+ const top = matches[0]
851
+ const topScore = Number(top.score) || 0
852
+ if (currentPath !== null && top.path === currentPath) return { kind: 'open', match: top }
853
+ const isStrong = (item) => GUFA_DEFINITION_STRONG_KINDS.has(item.kind)
854
+ if (!isStrong(top) && topScore < 50) return { kind: 'none' }
855
+ if (topScore < 30) return { kind: 'none' }
856
+ const cluster = matches.filter((item) => (Number(item.score) || 0) >= topScore - 8).slice(0, 8)
857
+ if (cluster.length === 1) return { kind: 'open', match: cluster[0] }
858
+ const declCluster = cluster.filter((item) => isStrong(item))
859
+ if (declCluster.length === 0) {
860
+ const secondScore = matches.length > 1 ? Number(matches[1].score) || 0 : 0
861
+ return topScore >= 50 && topScore - secondScore >= 10 ? { kind: 'open', match: top } : { kind: 'none' }
862
+ }
863
+ const distinctFiles = new Set(declCluster.map((item) => item.path))
864
+ if (distinctFiles.size === 1) return { kind: 'open', match: declCluster[0] }
865
+ return { kind: 'pick', matches: declCluster }
866
+ }
867
+
868
+ /* ── Cmd 悬停:可跳转时下划线 + 手型光标 ─────────────── */
869
+ const hover = { meta: false, timer: 0, seq: 0, cache: new Map(), decorationIds: [], lastClient: null, lastPosition: null, lastWordInfo: null, lastKey: '' }
870
+ const clearHoverDecoration = () => {
871
+ if (hover.decorationIds.length > 0) {
872
+ try {
873
+ hover.decorationIds = instance.deltaDecorations(hover.decorationIds, [])
874
+ } catch {
875
+ hover.decorationIds = []
876
+ }
877
+ }
878
+ try {
879
+ editorDom.classList.remove('gufa-def-hover')
880
+ } catch {
881
+ /* ignore */
882
+ }
883
+ }
884
+ const showHoverDecoration = (position, wordInfo) => {
885
+ if (wordInfo === null || wordInfo === undefined) return clearHoverDecoration()
886
+ let range = null
887
+ try {
888
+ range = new monaco.Range(position.lineNumber, wordInfo.startColumn, position.lineNumber, wordInfo.endColumn)
889
+ } catch {
890
+ return clearHoverDecoration()
891
+ }
892
+ try {
893
+ hover.decorationIds = instance.deltaDecorations(hover.decorationIds, [
894
+ { range, options: { inlineClassName: 'gufa-def-link', stickiness: 1 } },
895
+ ])
896
+ } catch {
897
+ /* ignore */
898
+ }
899
+ try {
900
+ editorDom.classList.add('gufa-def-hover')
901
+ } catch {
902
+ /* ignore */
903
+ }
904
+ }
905
+ const evaluateHoverPosition = (position, wordInfo) => {
906
+ if (!hover.meta) {
907
+ clearHoverDecoration()
908
+ return
909
+ }
910
+ const model = instance.getModel()
911
+ const word = wordInfo !== null && wordInfo !== undefined && typeof wordInfo.word === 'string' ? wordInfo.word : ''
912
+ const path = modelPathRef.current
913
+ const root = modelsRootRef.current
914
+ if (position === null || position === undefined || model === null || model === undefined
915
+ || word.length < 2 || path === null || path === undefined || root === null || root === undefined || root === '') {
916
+ clearHoverDecoration()
917
+ return
918
+ }
919
+ hover.lastPosition = position
920
+ hover.lastWordInfo = wordInfo
921
+ const key = `${path}|${word}`
922
+ hover.lastKey = key
923
+ const cached = hover.cache.get(key)
924
+ if (cached === 'none') {
925
+ clearHoverDecoration()
926
+ return
927
+ }
928
+ if (cached === 'yes') {
929
+ showHoverDecoration(position, wordInfo)
930
+ return
931
+ }
932
+ if (hover.timer !== 0) {
933
+ window.clearTimeout(hover.timer)
934
+ hover.timer = 0
935
+ }
936
+ const seq = ++hover.seq
937
+ hover.timer = window.setTimeout(() => {
938
+ hover.timer = 0
939
+ if (!hover.meta || seq !== hover.seq) return
940
+ void queryDefinitions(position, word, model, path, root).then((result) => {
941
+ if (!hover.meta || seq !== hover.seq || hover.lastKey !== key) return
942
+ const decision = decideDefinition(result)
943
+ if (decision.kind === 'none') {
944
+ if (hover.cache.size > 100) hover.cache.clear()
945
+ hover.cache.set(key, 'none')
946
+ clearHoverDecoration()
947
+ return
948
+ }
949
+ if (hover.cache.size > 100) hover.cache.clear()
950
+ hover.cache.set(key, 'yes')
951
+ showHoverDecoration(position, wordInfo)
952
+ }).catch(() => {
953
+ /* ignore */
954
+ })
955
+ }, 130)
956
+ }
957
+ /* 用 Monaco 自己的 onMouseMove(带修饰键状态 + 精确 position),
958
+ 不依赖 DOM mousedown/getTargetAtClientPoint 的坐标系与包含关系。 */
959
+ const onMonacoMouseMove = (editorEvent) => {
960
+ if (editorEvent === null || editorEvent === undefined) return
961
+ const browser = editorEvent.event !== null && editorEvent.event !== undefined ? editorEvent.event.browserEvent : null
962
+ const mac = typeof G.isMacPlatform === 'function' ? G.isMacPlatform() : true
963
+ const pressed = browser !== null && browser !== undefined
964
+ ? (mac ? browser.metaKey === true : browser.ctrlKey === true)
965
+ : false
966
+ if (!pressed) {
967
+ if (hover.meta) {
968
+ hover.meta = false
969
+ hover.seq += 1
970
+ if (hover.timer !== 0) {
971
+ window.clearTimeout(hover.timer)
972
+ hover.timer = 0
973
+ }
974
+ clearHoverDecoration()
975
+ }
976
+ return
977
+ }
978
+ hover.meta = true
979
+ const target = editorEvent.target !== null && editorEvent.target !== undefined ? editorEvent.target : null
980
+ const position = target !== null ? target.position : null
981
+ if (position === null || position === undefined) {
982
+ clearHoverDecoration()
983
+ return
984
+ }
985
+ let wordInfo = null
986
+ try {
987
+ const model = instance.getModel()
988
+ wordInfo = model !== null && model !== undefined ? model.getWordAtPosition(position) : null
989
+ } catch {
990
+ wordInfo = null
991
+ }
992
+ evaluateHoverPosition(position, wordInfo)
993
+ }
994
+ const onMetaDown = (event) => {
995
+ if (event.key !== 'Meta' && event.key !== 'Control') return
996
+ if (hover.meta) return
997
+ hover.meta = true
998
+ if (hover.lastPosition !== null && hover.lastWordInfo !== null) evaluateHoverPosition(hover.lastPosition, hover.lastWordInfo)
999
+ }
1000
+ const onMetaUp = (event) => {
1001
+ if (event.key !== 'Meta' && event.key !== 'Control') return
1002
+ hover.meta = false
1003
+ hover.seq += 1
1004
+ if (hover.timer !== 0) {
1005
+ window.clearTimeout(hover.timer)
1006
+ hover.timer = 0
1007
+ }
1008
+ clearHoverDecoration()
1009
+ }
1010
+ const onDefWindowBlur = () => {
1011
+ hover.meta = false
1012
+ clearHoverDecoration()
1013
+ }
1014
+ const hoverSubscriptions = []
1015
+ try {
1016
+ if (typeof instance.onMouseMove === 'function') hoverSubscriptions.push(instance.onMouseMove(onMonacoMouseMove))
1017
+ if (typeof instance.onMouseLeave === 'function') hoverSubscriptions.push(instance.onMouseLeave(() => {
1018
+ hover.meta = false
1019
+ hover.lastPosition = null
1020
+ hover.lastWordInfo = null
1021
+ hover.seq += 1
1022
+ clearHoverDecoration()
1023
+ }))
1024
+ if (typeof instance.onDidChangeModelContent === 'function') hoverSubscriptions.push(instance.onDidChangeModelContent(() => clearHoverDecoration()))
1025
+ if (typeof instance.onDidScrollChange === 'function') hoverSubscriptions.push(instance.onDidScrollChange(() => clearHoverDecoration()))
1026
+ } catch {
1027
+ /* ignore */
1028
+ }
1029
+ for (const subscription of hoverSubscriptions) if (subscription !== null && subscription !== undefined) subscriptions.push(subscription)
758
1030
  const onDefinitionClick = async (event) => {
759
1031
  try {
760
1032
  const currentDom = typeof instance.getDomNode === 'function' ? instance.getDomNode() : null
@@ -763,9 +1035,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
763
1035
  return
764
1036
  }
765
1037
  if (event.button !== 0 || event.defaultPrevented === true) return
766
- const primary = typeof G.isMacPlatform === 'function' ? G.isMacPlatform() : true
767
- const pressed = primary ? event.metaKey === true : event.ctrlKey === true
768
- if (!pressed || event.altKey === true || event.shiftKey === true) return
1038
+ if (!isPrimaryEvent(event) || event.altKey === true || event.shiftKey === true) return
769
1039
  if (event.target instanceof Element && event.target.closest('a') !== null) return
770
1040
  let target = null
771
1041
  try {
@@ -786,13 +1056,18 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
786
1056
  if (word.length < 2) return
787
1057
  const path = modelPathRef.current
788
1058
  const root = modelsRootRef.current
789
- if (path === null || root === null || root === '') return
1059
+ if (path === null || path === undefined || root === null || root === undefined || root === '') return
790
1060
  try {
791
1061
  event.preventDefault()
792
1062
  event.stopPropagation()
793
1063
  } catch {
794
1064
  /* ignore */
795
1065
  }
1066
+ try {
1067
+ await preloadTsDependencies(model, path, root)
1068
+ } catch {
1069
+ /* ignore */
1070
+ }
796
1071
  const beforePath = modelPathRef.current
797
1072
  const beforePosition = instance.getPosition()
798
1073
  try {
@@ -815,55 +1090,44 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
815
1090
  } catch {
816
1091
  monacoHandled = false
817
1092
  }
1093
+ clearHoverDecoration()
818
1094
  if (monacoHandled) return
819
1095
  try {
820
- const result = await G.api('code.definition', {
821
- root,
822
- path,
823
- line: position.lineNumber,
824
- column: position.column,
825
- word,
826
- content: model.getValue(),
827
- })
828
- const matches = result !== null && result !== undefined && Array.isArray(result.matches) ? result.matches : []
829
- if (matches.length === 0) return
830
- const openMatch = (item) => G.bus.emit('editor:open-at', { path: item.path, line: item.line, column: item.column })
831
- const currentPath = result.current !== null && result.current !== undefined && typeof result.current.path === 'string' ? result.current.path : null
832
- const top = matches[0]
833
- const topScore = Number(top.score) || 0
834
- /* 当前文件里的声明:直接跳,不打扰 */
835
- if (currentPath !== null && top.path === currentPath) {
836
- openMatch(top)
837
- return
838
- }
839
- const isStrong = (item) => GUFA_DEFINITION_STRONG_KINDS.has(item.kind)
840
- /* 弱匹配(const/赋值等)只有在很近/很确定时才跳,避免点属性名时跳到别的仓库同名变量 */
841
- if (!isStrong(top) && topScore < 50) return
842
- if (topScore < 30) return
843
- const cluster = matches.filter((item) => (Number(item.score) || 0) >= topScore - 8).slice(0, 8)
844
- if (cluster.length === 1) {
845
- openMatch(cluster[0])
1096
+ const result = await queryDefinitions(position, word, model, path, root)
1097
+ const decision = decideDefinition(result)
1098
+ if (decision.kind === 'open') {
1099
+ G.bus.emit('editor:open-at', { path: decision.match.path, line: decision.match.line, column: decision.match.column })
846
1100
  return
847
1101
  }
848
- const declCluster = cluster.filter((item) => isStrong(item))
849
- if (declCluster.length === 0) {
850
- const secondScore = matches.length > 1 ? Number(matches[1].score) || 0 : 0
851
- if (topScore >= 50 && topScore - secondScore >= 10) {
852
- openMatch(top)
853
- }
854
- return
855
- }
856
- const distinctFiles = new Set(declCluster.map((item) => item.path))
857
- if (distinctFiles.size === 1) {
858
- openMatch(declCluster[0])
859
- return
1102
+ if (decision.kind === 'pick') {
1103
+ G.bus.emit('definition:pick', {
1104
+ word: result !== null && result !== undefined && typeof result.word === 'string' ? result.word : word,
1105
+ matches: decision.matches,
1106
+ x: event.clientX,
1107
+ y: event.clientY,
1108
+ })
860
1109
  }
861
- G.bus.emit('definition:pick', { word: result.word ?? word, matches: declCluster, x: event.clientX, y: event.clientY })
862
1110
  } catch {
863
1111
  /* ignore */
864
1112
  }
865
1113
  }
866
1114
  document.addEventListener('mousedown', onDefinitionClick, true)
1115
+ document.addEventListener('keydown', onMetaDown, true)
1116
+ document.addEventListener('keyup', onMetaUp, true)
1117
+ window.addEventListener('blur', onDefWindowBlur)
1118
+ definitionCleanup = () => {
1119
+ document.removeEventListener('mousedown', onDefinitionClick, true)
1120
+ document.removeEventListener('keydown', onMetaDown, true)
1121
+ document.removeEventListener('keyup', onMetaUp, true)
1122
+ window.removeEventListener('blur', onDefWindowBlur)
1123
+ if (hover.timer !== 0) {
1124
+ window.clearTimeout(hover.timer)
1125
+ hover.timer = 0
1126
+ }
1127
+ hover.seq += 1
1128
+ hover.meta = false
1129
+ clearHoverDecoration()
1130
+ }
867
1131
  }
868
1132
  } catch {
869
1133
  /* ignore */