php-doc.mi?

php-doc.vimの中途半端なパクリ
phpdoc を挿入してくれるスクリプト - 呆備録

property TYPE_MIXED : "mixed"
property TYPE_BOOL : "bool"
property TYPE_ARRAY : "array"
property TYPE_STR : "string"
on run
  tell application "mi"
    tell document 1
      set theHeadLine to (paragraphs of selection object 1)
      set firstpara to index of paragraph 1 of selection object 1
      set theHeadLine to theHeadLine as Unicode text
      
      if theHeadLine contains "function" then
        set theComment to getFuncComment(theHeadLine) of me
        set theInsertPos to firstpara
      else if theHeadLine contains "var" then
        set theComment to getVarComment(theHeadLine) of me
        set theInsertPos to firstpara
      else
        set theComment to get3LinesComment(theHeadLine) of me
        set theInsertPos to firstpara + 1
      end if
      
      --display dialog theComment
      -- 書き出し
      copy theComment to insertion point 1 of paragraph theInsertPos
      
      -- カーソールの移動
      set theCurPos to theInsertPos + 1
      make new word at after last word of paragraph theCurPos with data " "
    end tell
  end tell
end run

on getFuncComment(theHeadLine)
  set theRegx to "^(??s*)function??s+(.+)??s*??(??s*(.*)??s*??)"
  set theMatches to "$1#$2#$3"
  set {theIndent, theName, theParams} to parseHeadLine(theHeadLine, theRegx, theMatches)
  return getFuncCommentStr(theIndent, theName, theParams)
end getFuncComment

on getVarComment(theHeadLine)
  set theRegx to "^(??s*)var??s+??$(??w+)??s*=?(?:??s*(.+))???s*;"
  set theMatches to "$1#$2#$3"
  set {theIndent, theName, theVal} to parseHeadLine(theHeadLine, theRegx, theMatches)
  return getVarCommentStr(theIndent, theName, theVal)
end getVarComment

on get3LinesComment(theHeadLine)
  set theRegx to "^(??s*)"
  set theMatches to "$1"
  set {theLine} to parseHeadLine(theHeadLine, theRegx, theMatches)
  return get3LinesCommentStr(theLine)
end get3LinesComment

on parseHeadLine(theStr, theRegx, theMatches)
  set theRes to do shell script ("perl -e '$str=q{" & theStr & "};$str=~/" & theRegx & "/;print qq{" & theMatches & "};'")
  --display dialog theRes
  return devText(theRes, "#")
end parseHeadLine


on get3LinesCommentStr(theIndent)
  set theStr to theIndent & "/**" & return
  set theStr to theStr & theIndent & " *" & return
  set theStr to theStr & theIndent & " */" & return
end get3LinesCommentStr

on getFuncCommentStr(theIndent, funcName, theParams)
  set theParamList to getParamList(theParams) of me
  set theRet to getTypeFromName(funcName) of me
  
  -- 書き出す文字列
  set theStr to theIndent & "/**" & return
  set theStr to theStr & theIndent & " *" & return
  set theStr to theStr & theIndent & " * " & return
  
  if (number of theParamList > 0) then
    repeat with theParam in theParamList
      set theStr to theStr & theIndent & " * @params " & vartype of theParam
      set theStr to theStr & space & name of theParam & space & return
    end repeat
  end if
  
  set theStr to theStr & theIndent & " * @return " & theRet & " " & return
  
  set theAccess to getAccessType(funcName)
  set theStr to theStr & theIndent & " * @access " & theAccess & return
  set theStr to theStr & theIndent & " */" & return
end getFuncCommentStr

on getVarCommentStr(theIndent, theVarName, theVal)
  set theStr to theIndent & "/**" & return
  set theStr to theStr & theIndent & " *" & return
  --set theStr to theStr & theIndent & " * " & return
  
  if theVal is equal to "" then
    set theType to getTypeFromName(theVarName)
  else
    set theType to getTypeFromVal(theVal)
  end if
  set theStr to theStr & theIndent & " * @var  " & theType & return
  
  set theAccess to getAccessType(theVarName)
  set theStr to theStr & theIndent & " * @access " & theAccess & return
  set theStr to theStr & theIndent & " */" & return
end getVarCommentStr



on getAccessType(str)
  if str starts with "_" then
    return "private"
  else
    return "public"
  end if
end getAccessType

on getParamList(theStr)
  set theArg to replaceText(theStr, space, "")
  
  if theArg = "" or theArg = " " then
    return {}
  end if
  
  if theArg does not contain "," then
    set ret to getParamRec(theArg)
    return {ret}
  end if
  
  set theRet to {}
  set theList to devText(theArg, ",")
  repeat with s in theList
    set end of theRet to getParamRec(s)
  end repeat
  
  return theRet
end getParamList

on getParamRec(theStr)
  set theName to getParamName(theStr)
  set theType to getParamType(theStr)
  return {name:theName, vartype:theType}
end getParamRec

on getParamName(theStr)
  if theStr does not contain "=" then
    return {theStr}
  end if
  set theList to devText(theStr, "=")
  return item 1 of theList
end getParamName

on getParamType(theStr)
  if theStr contains "=" then
    return getTypeFromVal(theStr)
  end if
  set theStr to replaceText(theStr, "$", "")
  set theStr to replaceText(theStr, "&", "")
  return getTypeFromName(theStr)
end getParamType

-- TODO typeの設定
on getTypeFromName(theStr)
  if theStr starts with "is" or theStr starts with "can" or theStr starts with "has" then
    return TYPE_BOOL
  end if
  return TYPE_MIXED
end getTypeFromName

on getTypeFromVal(theStr)
  set s to replaceText(theStr, "_", "")
  if theStr contains "true" or theStr contains "false" then
    return TYPE_BOOL
  else if theStr contains "array" then
    return TYPE_ARRAY
  end if
  return TYPE_MIXED
end getTypeFromVal


-- common 
on replaceText(theText, serchStr, replaceStr)
  set tmp to AppleScript's text item delimiters
  set AppleScript's text item delimiters to serchStr
  set theList to every text item of theText
  set AppleScript's text item delimiters to replaceStr
  set theText to theList as string
  set AppleScript's text item delimiters to tmp
  return theText
end replaceText

on devText(theText, aDelimiter)
  set tmp to AppleScript's text item delimiters
  set AppleScript's text item delimiters to aDelimiter
  set theList to every text item of theText
  set AppleScript's text item delimiters to tmp
  return theList
end devText