@@ -186,6 +186,11 @@ fn truncate(text: &str, bytes: usize) -> String {
186186 text[ ..end] . to_owned ( )
187187}
188188
189+ pub ( crate ) fn clean_text ( text : & str , bytes : usize ) -> String {
190+ let cleaned: String = text. chars ( ) . filter ( |ch| !ch. is_control ( ) ) . collect ( ) ;
191+ truncate ( & cleaned, bytes)
192+ }
193+
189194/// The last segment of a slash or backslash separated path, and what precedes it.
190195fn split_path ( path : & str ) -> ( & str , & str ) {
191196 let trimmed = path. trim_end_matches ( [ '/' , '\\' ] ) ;
@@ -682,6 +687,296 @@ pub fn project_name(module: &Path) -> String {
682687 . unwrap_or ( folder)
683688}
684689
690+ /// IDEA modules are displayed as declared. JDT resolves their libraries in
691+ /// one unmanaged workspace project, so effective paths are workspace facts.
692+ pub ( crate ) fn idea_fields (
693+ zh : bool ,
694+ root : & Path ,
695+ module : & crate :: project_model:: IdeaModule ,
696+ classpaths : Option < & [ String ] > ,
697+ ) -> Vec < Value > {
698+ use crate :: project_model:: Origin ;
699+ use std:: collections:: BTreeSet ;
700+
701+ let source_section = local ( zh, "Sources" , "源" ) ;
702+ let paths_section = local ( zh, "Paths" , "路径" ) ;
703+ let dependencies_section = local ( zh, "Dependencies" , "依赖" ) ;
704+ let declared = local ( zh, "IDEA declaration" , "IDEA 声明" ) ;
705+ let effective: Option < BTreeSet < _ > > = classpaths. map ( |paths| {
706+ paths
707+ . iter ( )
708+ . map ( |path| classpath_key ( Path :: new ( path) ) )
709+ . collect ( )
710+ } ) ;
711+ let row = |kind : & str , value : & str , title : & str , detail : & str | {
712+ json ! ( {
713+ "kind" : kind,
714+ "value" : clean_text( value, MAX_VALUE ) ,
715+ "title" : clean_text( title, 256 ) ,
716+ "detail" : clean_text( detail, 1024 ) ,
717+ } )
718+ } ;
719+ let kinds = [
720+ ( FOLDER , "Source folder" , "源码文件夹" , & module. sources ) ,
721+ ( "test" , "Test source" , "测试源码" , & module. test_sources ) ,
722+ ( "resource" , "Resource" , "资源" , & module. resources ) ,
723+ (
724+ "test-resource" ,
725+ "Test resource" ,
726+ "测试资源" ,
727+ & module. test_resources ,
728+ ) ,
729+ ( "excluded" , "Excluded" , "排除" , & module. excluded ) ,
730+ ] ;
731+ let mut seen_roots = BTreeSet :: new ( ) ;
732+ let mut roots = Vec :: new ( ) ;
733+ for ( kind, en, cn, paths) in & kinds {
734+ let detail = local ( zh, en, cn) ;
735+ for raw in * paths {
736+ if roots. len ( ) == MAX_ENTRIES {
737+ break ;
738+ }
739+ let value = clean_text ( raw, MAX_VALUE ) ;
740+ if value. trim ( ) . is_empty ( ) || !seen_roots. insert ( ( * kind, value. clone ( ) ) ) {
741+ continue ;
742+ }
743+ roots. push ( row ( kind, & value, & value, & detail) ) ;
744+ }
745+ }
746+ let root_kinds: Vec < _ > = kinds
747+ . iter ( )
748+ . map ( |( kind, en, cn, _) | json ! ( { "id" : kind, "title" : local( zh, en, cn) , "input" : "directory" } ) )
749+ . collect ( ) ;
750+ let content_roots: Vec < _ > = module
751+ . content_roots
752+ . iter ( )
753+ . filter_map ( |path| {
754+ let path = if path == "." {
755+ root. to_path_buf ( )
756+ } else {
757+ root. join ( path)
758+ } ;
759+ let cleaned = clean_text ( & path. display ( ) . to_string ( ) , MAX_VALUE ) ;
760+ ( !cleaned. trim ( ) . is_empty ( ) ) . then_some ( cleaned)
761+ } )
762+ . take ( MAX_ENTRIES )
763+ . collect ( ) ;
764+ let mut seen_deps = BTreeSet :: < ( & str , String ) > :: new ( ) ;
765+ let mut dependencies = Vec :: new ( ) ;
766+ for ( entry, directory) in module
767+ . libraries
768+ . iter ( )
769+ . map ( |item| ( item, false ) )
770+ . chain ( module. class_folders . iter ( ) . map ( |item| ( item, true ) ) )
771+ {
772+ if dependencies. len ( ) == MAX_ENTRIES {
773+ break ;
774+ }
775+ let pattern = clean_text ( entry. pattern . as_str ( ) , MAX_VALUE ) ;
776+ if pattern. trim ( ) . is_empty ( ) {
777+ continue ;
778+ }
779+ let glob = pattern
780+ . strip_suffix ( "/**/*.jar" )
781+ . map ( |base| ( base, true ) )
782+ . or_else ( || pattern. strip_suffix ( "/*.jar" ) . map ( |base| ( base, false ) ) ) ;
783+ let ( kind, type_name, title) = if let Some ( ( base, _) ) = glob {
784+ (
785+ LIBRARY ,
786+ local ( zh, "JAR directory" , "JAR 库目录" ) ,
787+ split_path ( base) . 0 ,
788+ )
789+ } else if directory {
790+ (
791+ CLASSES ,
792+ local ( zh, "Class folder" , "类目录" ) ,
793+ split_path ( & pattern) . 0 ,
794+ )
795+ } else {
796+ (
797+ ARCHIVE ,
798+ local ( zh, "Archive" , "归档" ) ,
799+ split_path ( & pattern) . 0 ,
800+ )
801+ } ;
802+ if !seen_deps. insert ( ( kind, pattern. clone ( ) ) ) {
803+ continue ;
804+ }
805+ let source = if entry. origin == Origin :: IdeaProjectLibrary {
806+ local ( zh, "IDEA project library" , "IDEA 项目库" )
807+ } else {
808+ local ( zh, "IDEA module library" , "IDEA 模块库" )
809+ } ;
810+ let status = effective. as_ref ( ) . map_or_else (
811+ || local ( zh, "JDT status unconfirmed" , "JDT 状态未确认" ) ,
812+ |paths| {
813+ if let Some ( ( base, recursive) ) = glob {
814+ let prefix =
815+ format ! ( "{}/" , classpath_key( & root. join( base) ) . trim_end_matches( '/' ) ) ;
816+ let matches = paths
817+ . iter ( )
818+ . filter ( |path| {
819+ path. strip_prefix ( & prefix) . is_some_and ( |tail| {
820+ tail. to_ascii_lowercase ( ) . ends_with ( ".jar" )
821+ && ( recursive || !tail. contains ( '/' ) )
822+ } )
823+ } )
824+ . count ( ) ;
825+ if matches > 0 {
826+ return if zh {
827+ format ! ( "JDT 已加载 {matches} 个 JAR" )
828+ } else {
829+ format ! ( "{matches} JARs loaded by JDT" )
830+ } ;
831+ }
832+ } else if paths. contains ( & classpath_key ( & root. join ( & pattern) ) ) {
833+ return local ( zh, "Loaded by JDT" , "JDT 已生效" ) ;
834+ }
835+ local ( zh, "Not on JDT classpath" , "未进入 JDT 类路径" )
836+ } ,
837+ ) ;
838+ let detail = format ! (
839+ "{type_name} · {source} ({}) · {status} · {pattern}" ,
840+ entry. label
841+ ) ;
842+ dependencies. push ( row ( kind, & pattern, title, & detail) ) ;
843+ }
844+ let module_detail = local (
845+ zh,
846+ "IDEA module dependency (declared)" ,
847+ "IDEA 模块依赖(原工程声明)" ,
848+ ) ;
849+ for name in & module. module_dependencies {
850+ if dependencies. len ( ) == MAX_ENTRIES {
851+ break ;
852+ }
853+ let value = clean_text ( name, MAX_VALUE ) ;
854+ if value. trim ( ) . is_empty ( ) || !seen_deps. insert ( ( MODULE , value. clone ( ) ) ) {
855+ continue ;
856+ }
857+ dependencies. push ( row ( MODULE , & value, & value, & module_detail) ) ;
858+ }
859+ for item in & module. unresolved {
860+ if dependencies. len ( ) == MAX_ENTRIES {
861+ break ;
862+ }
863+ use crate :: project_model:: UnresolvedKind ;
864+ let reason = match item. kind {
865+ UnresolvedKind :: MissingFile => local ( zh, "Missing path" , "路径不存在" ) ,
866+ UnresolvedKind :: MissingLibrary => local ( zh, "Undefined library" , "找不到项目库定义" ) ,
867+ UnresolvedKind :: IdeLibrary => local (
868+ zh,
869+ "IDEA library outside the project" ,
870+ "IDEA 全局库或应用库" ,
871+ ) ,
872+ UnresolvedKind :: UnknownMacro => {
873+ local ( zh, "Unknown IDEA path variable" , "IDEA 路径变量未解析" )
874+ }
875+ UnresolvedKind :: OutsideWorkspace => continue ,
876+ UnresolvedKind :: UserLibrary => continue ,
877+ } ;
878+ let value = clean_text ( & item. name , MAX_VALUE ) ;
879+ if value. trim ( ) . is_empty ( ) || !seen_deps. insert ( ( LIBRARY , value. clone ( ) ) ) {
880+ continue ;
881+ }
882+ dependencies. push ( row ( LIBRARY , & value, & value, & reason) ) ;
883+ }
884+ let output_source = if module. inherits_output {
885+ local ( zh, "Inherited from IDEA project" , "继承 IDEA 项目输出" )
886+ } else {
887+ declared. clone ( )
888+ } ;
889+ let output = module
890+ . output
891+ . as_deref ( )
892+ . map ( |path| clean_text ( path, MAX_VALUE ) )
893+ . filter ( |path| !path. trim ( ) . is_empty ( ) ) ;
894+ let test_output = module
895+ . test_output
896+ . as_deref ( )
897+ . map ( |path| clean_text ( path, MAX_VALUE ) )
898+ . filter ( |path| !path. trim ( ) . is_empty ( ) ) ;
899+ let sdk = clean_text (
900+ module. sdk . as_deref ( ) . unwrap_or ( & local (
901+ zh,
902+ "Inherited from IDEA project" ,
903+ "继承 IDEA 项目 SDK" ,
904+ ) ) ,
905+ MAX_VALUE ,
906+ ) ;
907+ let descriptor = clean_text ( & module. descriptor , 1024 ) ;
908+ let classpath_status = if effective. is_some ( ) {
909+ local (
910+ zh,
911+ "JDT returned the workspace classpath" ,
912+ "JDT 已返回工作区类路径" ,
913+ )
914+ } else {
915+ local (
916+ zh,
917+ "JDT classpath unconfirmed. Reload after indexing finishes." ,
918+ "JDT 类路径未确认。索引结束后可点「重新加载」。" ,
919+ )
920+ } ;
921+ vec ! [
922+ json!( { "id" : "content-roots" , "title" : local( zh, "Content roots" , "内容根" ) , "section" : source_section, "kind" : "stringList" ,
923+ "value" : content_roots, "effectiveValue" : content_roots, "effectiveSource" : declared} ) ,
924+ json!( { "id" : ROOTS_FIELD , "title" : local( zh, "Source folders" , "源码文件夹" ) , "section" : source_section, "kind" : "entryList" ,
925+ "value" : roots, "effectiveValue" : roots, "effectiveSource" : declared,
926+ "entryKinds" : root_kinds} ) ,
927+ json!( { "id" : OUTPUT_FIELD , "title" : local( zh, "IDEA output" , "IDEA 输出目录" ) , "section" : paths_section, "kind" : "text" ,
928+ "value" : output, "effectiveValue" : Value :: Null , "effectiveSource" : output_source} ) ,
929+ json!( { "id" : "test-output" , "title" : local( zh, "IDEA test output" , "IDEA 测试输出目录" ) , "section" : paths_section, "kind" : "text" ,
930+ "value" : test_output, "effectiveValue" : Value :: Null , "effectiveSource" : output_source} ) ,
931+ json!( { "id" : JRE_FIELD , "title" : local( zh, "IDEA SDK" , "IDEA SDK" ) , "section" : dependencies_section, "kind" : "text" ,
932+ "value" : sdk, "effectiveValue" : Value :: Null , "effectiveSource" : declared} ) ,
933+ json!( { "id" : "jdt-status" , "title" : local( zh, "JDT classpath status" , "JDT 类路径状态" ) , "section" : dependencies_section, "kind" : "text" ,
934+ "value" : classpath_status, "effectiveValue" : classpath_status, "effectiveSource" : "JDT" } ) ,
935+ json!( { "id" : DEPENDENCIES_FIELD , "title" : dependencies_section, "section" : dependencies_section, "kind" : "entryList" ,
936+ "description" : local( zh, "Read from the IDEA module. JDT status refers to the workspace compile classpath. Edit in IDEA or add workspace libraries in Referenced libraries (.vscode/settings.json)." ,
937+ "读取 IDEA 模块声明。生效状态以 JDT 工作区类路径为准。可在 IDEA 修改原配置,或在「引用的库」补充工作区配置(.vscode/settings.json)。" ) ,
938+ "value" : dependencies, "effectiveValue" : dependencies, "effectiveSource" : descriptor,
939+ "entryKinds" : [
940+ { "id" : ARCHIVE , "title" : local( zh, "Archive" , "归档" ) , "input" : "file" } ,
941+ { "id" : CLASSES , "title" : local( zh, "Class folder" , "类目录" ) , "input" : "directory" } ,
942+ { "id" : LIBRARY , "title" : local( zh, "Library" , "库" ) , "input" : "choice" , "choices" : [ ] } ,
943+ { "id" : MODULE , "title" : local( zh, "Module dependency" , "模块依赖" ) , "input" : "choice" , "choices" : [ ] }
944+ ] } ) ,
945+ ]
946+ }
947+
948+ fn normalize_lexical ( path : & Path ) -> std:: path:: PathBuf {
949+ use std:: path:: Component ;
950+ let mut parts = Vec :: new ( ) ;
951+ for component in path. components ( ) {
952+ match component {
953+ Component :: CurDir => { }
954+ Component :: ParentDir => {
955+ if matches ! ( parts. last( ) , Some ( Component :: Normal ( _) ) ) {
956+ parts. pop ( ) ;
957+ } else if !matches ! (
958+ parts. last( ) ,
959+ Some ( Component :: RootDir | Component :: Prefix ( _) )
960+ ) {
961+ parts. push ( component) ;
962+ }
963+ }
964+ other => parts. push ( other) ,
965+ }
966+ }
967+ parts. into_iter ( ) . collect ( )
968+ }
969+
970+ pub ( crate ) fn classpath_key ( path : & Path ) -> String {
971+ let resolved = dunce:: canonicalize ( path) . unwrap_or_else ( |_| normalize_lexical ( path) ) ;
972+ let path = resolved. to_string_lossy ( ) . replace ( '\\' , "/" ) ;
973+ if cfg ! ( windows) {
974+ path. to_lowercase ( )
975+ } else {
976+ path
977+ }
978+ }
979+
685980#[ cfg( test) ]
686981mod tests {
687982 use super :: * ;
@@ -890,4 +1185,68 @@ mod tests {
8901185 json!( [ { "kind" : "archive" , "value" : "/repo/a/1/a-1.jar" , "title" : "a-1.jar" , "detail" : "/repo/a/1" } , { "kind" : "classes" , "value" : classes. to_string_lossy( ) , "title" : "core/target/classes" } ] )
8911186 ) ;
8921187 }
1188+
1189+ #[ test]
1190+ fn idea_fields_deduplicates_entries_and_strips_invalid_values ( ) {
1191+ use crate :: project_model:: { Detected , IdeaModule , Origin , Unresolved , UnresolvedKind } ;
1192+ let module = IdeaModule {
1193+ descriptor : "app\n .iml" . into ( ) ,
1194+ name : "app" . into ( ) ,
1195+ sources : vec ! [ "src" . into( ) , "src" . into( ) , "" . into( ) , "\t " . into( ) ] ,
1196+ test_sources : vec ! [ "test" . into( ) ] ,
1197+ content_roots : vec ! [ "." . into( ) , " " . into( ) ] ,
1198+ resources : vec ! [ ] ,
1199+ test_resources : vec ! [ ] ,
1200+ excluded : vec ! [ ] ,
1201+ libraries : vec ! [ Detected {
1202+ pattern: "lib/*.jar" . into( ) ,
1203+ origin: Origin :: IdeaModuleLibrary ,
1204+ label: "libs" . into( ) ,
1205+ count: 1 ,
1206+ } ] ,
1207+ class_folders : vec ! [ ] ,
1208+ module_dependencies : vec ! [ "core" . into( ) , "core" . into( ) , "" . into( ) ] ,
1209+ unresolved : vec ! [
1210+ Unresolved {
1211+ kind: UnresolvedKind :: MissingLibrary ,
1212+ name: "lib/*.jar" . into( ) ,
1213+ } ,
1214+ Unresolved {
1215+ kind: UnresolvedKind :: MissingFile ,
1216+ name: "missing.jar" . into( ) ,
1217+ } ,
1218+ Unresolved {
1219+ kind: UnresolvedKind :: MissingFile ,
1220+ name: "missing.jar" . into( ) ,
1221+ } ,
1222+ Unresolved {
1223+ kind: UnresolvedKind :: MissingFile ,
1224+ name: " " . into( ) ,
1225+ } ,
1226+ ] ,
1227+ output : Some ( "out\0 " . into ( ) ) ,
1228+ test_output : Some ( " " . into ( ) ) ,
1229+ inherits_output : false ,
1230+ sdk : None ,
1231+ } ;
1232+ let fields = idea_fields ( false , Path :: new ( "/workspace" ) , & module, None ) ;
1233+ let field = |id| fields. iter ( ) . find ( |item| item[ "id" ] == id) . unwrap ( ) ;
1234+ let roots = field ( ROOTS_FIELD ) [ "value" ] . as_array ( ) . unwrap ( ) ;
1235+ assert_eq ! ( roots. len( ) , 2 ) ;
1236+ let deps = field ( DEPENDENCIES_FIELD ) [ "value" ] . as_array ( ) . unwrap ( ) ;
1237+ let keys: std:: collections:: BTreeSet < _ > = deps
1238+ . iter ( )
1239+ . map ( |item| {
1240+ (
1241+ item[ "kind" ] . as_str ( ) . unwrap ( ) . to_owned ( ) ,
1242+ item[ "value" ] . as_str ( ) . unwrap ( ) . to_owned ( ) ,
1243+ )
1244+ } )
1245+ . collect ( ) ;
1246+ assert_eq ! ( deps. len( ) , 3 ) ;
1247+ assert_eq ! ( keys. len( ) , 3 ) ;
1248+ assert_eq ! ( field( OUTPUT_FIELD ) [ "value" ] , "out" ) ;
1249+ assert ! ( field( "test-output" ) [ "value" ] . is_null( ) ) ;
1250+ assert_eq ! ( field( DEPENDENCIES_FIELD ) [ "effectiveSource" ] , "app.iml" ) ;
1251+ }
8931252}
0 commit comments